2

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
5
  • Do you want to simply cast it, or are you trying to pack multiple chars into each int? Commented Aug 29, 2011 at 22:29
  • 3
    Which of the billions of unsigned integers do you want to turn that array into? Commented Aug 29, 2011 at 22:35
  • 1
    Your question is very poorly worded. Please tell us what exactly you're trying to do so we can help you. Commented Aug 29, 2011 at 22:36
  • 6 byte integer? Maybe the first few bytes are something else and it is a 4 byte integer. Commented Aug 29, 2011 at 22:37
  • 1
    I want to get one unsigned integer. If the data file would contain 00 00 23 51 A4 D2, then the integer should be 592'553'170. Commented Aug 29, 2011 at 22:37

5 Answers 5

3

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];
Sign up to request clarification or add additional context in comments.

2 Comments

The first two assignments have no effect. The other six could be better written as a loop: for (i = 0; i < 6; ++i) { tindex = (tindex << 8) + table_index[i]; }
What's the little endian versus big endian implications of this? My binary stream is in big endian (network) and my system is little endian. I'm getting the proper results for numbers from this, but wonder if that would change if on big endian.
2

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

2 Comments

... and also assuming he's on a big-endian system... which given the prevalence of Intel CPUs these days is probably a bad assumption.
It gives me 3746994889972252672
0

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.

1 Comment

The endianness you're looking for is called big.
0

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];

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.