简体   繁体   中英

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 . 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. There is probably a 64 bit type though, and it might be a "long long".

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:

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

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:

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 :

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 :

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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