I am trying to decode some data sent from a big-endian machine to the decoder, which is residing on a little-endian machine. I haven't worked with this very much, and I feel like I am confusing myself.
I use bitsets to print my data so that I can see exactly how it is coming out for a particular 32 bit structure, and I can see that the data I need is in the middle of the bit sequence.
Now, I know that if you have a 32-bit value, to go from big to little, you reverse the byte ordering. If I do that, my numbers are not ending up where I expect them to be (done by hand).
So, for example. I have a 32-bit unsigned int. I know that it is coming from my Big-Endian machine as 0x50000000. When I print this using the bitset on the little-endian machine cout << "packSpare 32: " << bitset<32>(Data.pack_Spare).to_string() << endl;
I get 0x00005000. So it looks more like it swapped the first two bytes, in order, with the second two bytes, in order.
I originally had a struct like this:
#pragma pack(push, 1)
struct PACK_SPARE
{
int Spare3:28;
int Heading_Reference:1;
int Spare2:1;
int H_Valid:1;
int Spare5:1;
};
#pragma pack(pop)
Which is in the reverse order of how it is sent from the big endian machine, but I noticed the issure regarding some obvious swapping of bits happening, so I wanted to just pull the whole thing in as 32 bits, then swap, then print the data. Now I am just using int pack_Spare;
Is it just taking the two 16 bit chunks, and swapping those, rather than doing the swap on the entire 32-bit value? Sorry if this doesn't make sense, like I said, I am kind of confused.
EDIT This data is not coming over a network. I am streaming bits from a video file. I store this data into its corresponding values. So I guess my question is if I have a 32 bit int, and then fread data into that variable, if I use bitset, why is it swapping the two 16-bit groupings of my 32 bit int, rather than doing it by byte? I'm expecting 0x50000000, but I get 0x00005000 (0000 and 5000 got swapped, instead of what I was expecting for typical endian swapping, which was reversing the order of all bytes).
Although I don't often recommend Wikipedia for anything beyond introduction, I did find a good endian discussion here, (In answer to your question why is it swapping the two 16-bit groupings of my 32 bit int, rather than doing it by byte?).
The discussion occurs about 2/3 down the page of that link... Look for this:
There are several interesting things regarding endian on that same page.