[英]Reading 6 byte 8-bit integer from binary file
This is what my file looks like: 这是我的文件的样子:
00 00 00 00 00 34 ....
I have read it already to a unsigned char
array using fread
, but I don't know, how I can now turn it into a unsigned integer
. 我已经使用
fread
将其读取到unsigned char
数组中,但是我不知道如何将其转换为unsigned integer
。 The array looks like this: 该数组如下所示:
0, 0, 0, 0, 0, 52
This is how I got it to work: 这就是我的工作方式:
unsigned char table_index[6];
fread(table_index, 1, 6, file);
unsigned long long tindex = 0;
tindex = (tindex << 8);
tindex = (tindex << 8);
tindex = (tindex << 8) + table_index[0];
tindex = (tindex << 8) + table_index[1];
tindex = (tindex << 8) + table_index[2];
tindex = (tindex << 8) + table_index[3];
tindex = (tindex << 8) + table_index[4];
tindex = (tindex << 8) + table_index[5];
You're starting with a 48 bit value but there's probably no 48 bit integer type on your system. 您从48位值开始,但是系统上可能没有48位整数类型。 There is probably a 64 bit type though, and it might be a "long long".
但是可能有64位类型,并且可能是“长长”类型。
Assuming your 6 bytes are ordered most significant first, and understanding that you need to fill out two extra bytes for a long long, you might do something such as: 假设您的6个字节排在最前面,并且理解您需要长时间填写两个额外的字节,则可以执行以下操作:
long long myNumber;
char *ptr = (char *)&myNumber;
*ptr++ = 0; // pad the msb
*ptr++ = 0; // pad the 2nd msb
fread(ptr, 1, 6, fp);
Now you've got a value in myNumber
现在,您在
myNumber
有了一个值
If the file is filled with 48-bit integers like I am assuming you are talking about, from the char array, you can do this: 如果文件充满了48位整数(例如我假设您正在谈论的内容),则可以从char数组中执行以下操作:
char temp[8];
unsigned char *data = //...
unsigned char *data_ptr = data;
vector<unsigned long long> numbers;
size_t sz = // Num of 48-bit numbers
for (size_t i = 0; i < sz; i++, data_ptr += 6)
{
memcpy(temp + 2, data_ptr, 6);
numbers.push_back((unsigned long long)*temp);
}
This algorithm assumes that the numbers are all already encoded properly in the file. 该算法假定数字均已在文件中正确编码。 It also assumes an endianness that I cannot name off the top of my head.
它还假定我不能说出我的头名。
if you want to interpret 4 bytes of your uchar array as one uint do this : 如果要将uchar数组的4个字节解释为一个uint,请执行以下操作:
unsigned char uchararray[totalsize];
unsigned int * uintarray = (unsigned int *)uchararray;
if you want one byte of your uchar array to be transformed to one uint do this : 如果您希望将uchar数组的一个字节转换为一个uint,请执行以下操作:
unsigned char uchararray[totalsize];
unsigned int uintarray[totalsize];
for(int i = 0 ; i < totalsize; i++)
uintarray[i] = (unsigned int)uchararray[i];
Is this what you're talking about? 这是你在说什么吗?
// long long because it's usually 8 bytes (and there's not usually a 6 byte int type)
vector<unsigned long long> numbers;
fstream infile("testfile.txt");
if (!infile) {
cout << "fail" << endl;
cin.get();
return 0;
}
while (true) {
stringstream numstr;
string tmp;
unsigned long long num;
for (int i = 0; i < 6 && infile >> tmp; ++i)
numstr << hex << tmp;
if (cin.bad())
break;
cout << numstr.str() << endl;
numstr >> num;
numbers.push_back(num);
}
I tested it with the input you gave ( 00 00 23 51 A4 D2
) and the contents of the vector were 592553170
. 我用您提供的输入(
00 00 23 51 A4 D2
)对其进行了测试,向量的内容为592553170
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.