Linux-SWAT
Forum Addict!
- Joined
- Feb 13, 2010
- Messages
- 9,188
Hi,
I've used fopen with the b flag on a binary file.
I can read the octets (8 bit bytes) one by one from the start with a loop, no problem.
But let's say I have at the start of the file:
12 23 34 45 56 67 78 89 90 etc.
I need to read the little endian data part corresponding to
89 78 67 56
And store it into an int or a string or whatever.
What is the proper way to do that ?
I've encountered some trouble like:
-when looping, file[0] is not a char, it's an octet (two chars) so I had problems using memcpy() (segfault)
-using just printf, 00 appear to be 0, 01 -> 1, which is normal but I fear it can be troublesome
-at some point I wondered if I should add every octet after having multiplied it before with its weight (very naive example e.g. in order 11 22 = 11*100 + 22). I guess this is an awful idea but I was stuck (and I am still).
-htonl() is sexy but I don't see if it can be useful in this context
-I also wondered if it can be more natural to go to the binary level
I've used fopen with the b flag on a binary file.
I can read the octets (8 bit bytes) one by one from the start with a loop, no problem.
But let's say I have at the start of the file:
12 23 34 45 56 67 78 89 90 etc.
I need to read the little endian data part corresponding to
89 78 67 56
And store it into an int or a string or whatever.
What is the proper way to do that ?
I've encountered some trouble like:
-when looping, file[0] is not a char, it's an octet (two chars) so I had problems using memcpy() (segfault)
-using just printf, 00 appear to be 0, 01 -> 1, which is normal but I fear it can be troublesome
-at some point I wondered if I should add every octet after having multiplied it before with its weight (very naive example e.g. in order 11 22 = 11*100 + 22). I guess this is an awful idea but I was stuck (and I am still).
-htonl() is sexy but I don't see if it can be useful in this context
-I also wondered if it can be more natural to go to the binary level