I have a problem with the way integers are saved to memory. I want to read in binary from a file that contains some ints and other kinds of variables. I read every byte one by one, not four by four. I use this way to try to do a kind of file copy app by myself. But then it seems that the data is reversed.
Lets say I want to store integer number 1 on a file, I hope it is in binary:
Code:
00000000 00000000 00000000 00000001
but then when I read it one byte at a time, I'd get this:
Code:
00000001 00000000 00000000 00000000
So it seem to me that things are reversed, and I found out the same while trying to do the same directly on some stacked memory... But is there any reason for this? Can it just be my compiler? Anyway, this is what is making the thing (well that's a sentence..):
Code:
int a = 1;
UCHAR *b;
b = (UCHAR*)&a;
std::cout << (int)(*(UCHAR*)b)
<< (int)(*(UCHAR*)(b + 1))
<< (int)(*(UCHAR*)(b + 2))
<< (int)(*(UCHAR*)(b + 3));
//Displays 1000... Not 0001