Reputation: 61289
Is there a set of C library functions (or non-library functions collected somewhere) which allow conversion of byte-array data into integers with various conversions (big-endian data, little-endian data, one's complement signing, two's complement signing, et cetera), taking into account the machine's own endianness?
Thanks!
Upvotes: 4
Views: 774
Reputation: 1
As I understand, you are trying to get an unequivocal BIG_ENDIAN (or LITTLE_ENDIAN) number, without necessarily knowing what your current host's convention is. This should be possible.
To my best understanding, by convention "network byte order" is always BIG_ENDIAN. So you can call htonx(), then you know it is BIG_ENDIAN. Then if you want LITTLE_ENDIAN, you can manually transpose the bytes in your own code. The result should be your own toBigEndian() or toLittleEndian() functions.
The 2's complement part of the question does not admit of the same thing, since it is entirely a matter of convention. Say the number is two bytes long - almost every combination of 16 bit values is both a valid 1's complement number, and also a valid 2's complement number. The "correct" value depends entirely on the interpretation that was put on it by the code which wrote that value.
Upvotes: 0
Reputation: 5505
This comes close, although I don't think it will handle one's complement vs. two's complement. You might be able to add that. http://code.google.com/p/protobuf-c/
Upvotes: 1