0
我有一个是在六e.g表示手法与比特
4d42 b666 000a 0000 0000 0036 0000 0028
前4个字节表示,其我想提取值二进制文件。
我知道我可以用鼠标右键
std::ifstream is("img.bmp", std::ifstream::binary);
uint64_t data = 0;
is.read((char*)&data,4)
这将导致3060157762
但是使用
unsigned char * test = new unsigned char [ 4 ];
is.read((char*)test , 4);
uint64_t t = 0;
for(int i = 0; i < 4;i++){
t <<= 8; // 2 hexa symbols = 1 byte = 1 char = 8 bits
t|= test[i];
}
cout << t << endl;
这导致1112368822
这显然不同的解压缩。
我不知道我们怎么能用第二种方法实现相同的结果?什么是这样的一些按位技巧?除了我所展示的方法之外,我想不出任何东西。
感谢您的帮助。
提示:3060157762给112368822如果颠倒的字节顺序。查找[Endianness](https://en.wikipedia.org/wiki/Endianness) –
为什么使用8字节类型来读取4个字节?在可能会破坏你的程序的小端平台上。 – Slava
@Slava你的意思是unsigned char?它的范围是0-255,所以它应该是1个字节,或者我错了吗? – Darlyn