Hi,
Honestly, I'm not much of an endian-person so I can't answer your question and hopefully someone else can. I was answering your question about fread.
If your data is in this format:
<A Byte 0> <A Byte 1>|<B Byte 0> <B Byte 1>|<C Byte 0> <C Byte 1> <C Byte 2> <C Byte 3>|<D Byte 0> <D Byte 1> <D Byte 2> <D Byte 3>|<D Byte 0> <D Byte 1>|<D Byte 0> <D Byte 1>
How about for A, you do this instead:
1 2
|
unsigned short int bytes_read = 0;
fread (bytes_read, sizeof (unsigned short int), 1, handle);
| |
I think you're doing what you're doing because you want to use the same function for reading both 2-byte numbers and 4-byte numbers? You might save yourself a headache if you read in short ints for one and unsigned int for the other.
I
think endian is important if the computer that generated the data is of different endian from the machine that's reading the data. If you have four bytes: <A, B, C, D>, the reversed endian is <D, C, B, A> (I believe).
Also, in your code:
|
fread (bytes_read + (4 - len), sizeof (char), len, handle);
| |
If len=2 and bytes_read is an unsigned char array, you are writing what bytes 0 and 1 of A (from what I can tell), but:
* the first two bytes of bytes_read are uninitialized, unless you did it before
* you should pass the address of (bytes_read + (4 - len)) if you declared bytes_read as: unsigned int bytes_read.
I hope this helps or that someone else corrects me if I am wrong...
Ray