简体   繁体   English

在C ++中从big-endian二进制文件中检索int的“正确”方法

[英]“Right” way to retrieve an int from a big-endian binary file in c++

I have a binary file in big-endian format from which I am retrieving 2-bit and 4-bit integer data. 我有一个big-endian格式的二进制文件,我从中检索2位和4位整数数据。 The machine I'm running on is little-endian. 我正在运行的机器是little-endian。

Does anyone have any suggestions or a best-practice on pulling integer data from a known format binary and switching endianness on the fly? 有没有人有任何建议或最佳实践从已知格式二进制文件中提取整数数据并动态切换字节顺序? I'm not sure that my current solution is even correct: 我不确定我目前的解决方案是否正确:

int myInt;

ifstream dataFile(dataFileLocation, ios::in | ios::binary);
dataFile.seekg(99, ios::beg);  //Pull data starting at byte 100;

//For 4-byte value:
char chunk[4];
dataFile.read(chunk, 4);
myInt = (int)(chunk[0] << 24 | chunk[1] << 16 | chunk[2] << 8 | chunk[3]);

//For 2-byte value:
char chunk[2];
dataFile.read(chunk, 4);
myInt = (int)(chunk[0] << 8 | chunk[1]);

This seems to work fine for 2-byte data but gives what I believe are incorrect values on 4-byte data. 这似乎适用于2字节数据但是我认为4字节数据上的值不正确。 I've read about htonl() but from what I've read that's not a smart way to go for flexibility. 我已经读过关于htonl()的内容,但从我所读到的内容来看,这并不是一种灵活的方法。

Use unsigned integral types only and you'll be fine: 只使用无符号整数类型,你会没事的:

unsigned char buf[4];
infile.read(reinterpret_cast<char*>(buf), 4);

unsigned int b4 = (buf[0] << 24) + ... + (buf[3]);
unsigned int b2 = (buf[0] << 8) + (buf[1]);

Shifting involves type promotions, and indefinite sign extensions (given the implementation-defined nature of char ). 转移涉及类型促销和无限期符号扩展(给定char的实现定义性质)。 Basically you always want everything to be unsigned when manipulating bits. 基本上,你总是希望在操作位时一切都是无符号的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM