Ok, well I have done some research on this. And I have tried a few different methods, both seem to be giving me the same results, which must mean there is something else going on here that I am not understanding, or its just not working right.
EDIT: This piece of code provided the correct results
unsigned long int version = data.at(3) << 24 | data.at(2) << 16 | data.at(1) << 8 | data.at(0);
I used the code above instead of this and it worked.....but then that will mess up my stream i believe.
unsigned long int version; reader.read(reinterpret_cast<char*>(&version),sizeof(version));
I really want to understand what is going on here :) Don't mind reading something if it helps educate me :D
#END EDIT
I have 4 bytes
50 46 48 78
They should convert to the # 1311780402
My system seems to be reporting this as the wrong value. I am originally getting 0 instead of 1311780402. Well thats just plain wrong. So Mike suggested that it maybe an issue on my compiler with Big vs Little Endian. So I tried using 2 functions to re-analyze the bytes and correct the issue.
First is
// Define for bit changing below
#define is_bigendian() ( (*(char*)&i) == 0 )
unsigned long int reverseInt(unsigned long int i)
{
unsigned char c1, c2, c3, c4;
if (is_bigendian())
return i;
else
{
c1 = i & 255;
c2 = (i >> 8) & 255;
c3 = (i >> 16) & 255;
c4 = (i >> 24) & 255;
return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
}
}
version = reverseInt(version);
Second was
#include <intrin.h> // Used for endian support
version = _byteswap_ulong(version);
Both of these produced the same results (not the # I wanted) except one of them. (Going to ignore the 1 error for now).
So my question is this, are these 2 functions correct? If not why? If still not then how do i fix my problem? I am starting to think it is not 100% endian related, or there is another issue mixing with it...