Packing/unpacking binary data in C - doubles, 64 bits

Ben Scott dragonhawk at gmail.com
Thu Sep 10 14:54:36 EDT 2009


  We keep seeing the recommendation to use highly-portable encodings
when possible, e.g., ASCII, or some kind of self-descriptive encoding.
 Which I fully agree is a very good idea.

  But assume for the sake of discussion we want to keep overhead as
low as possible for performance reasons, and "wait until computers get
faster" isn't a practical solution.  What techniques, best practices,
de facto standards, popular libraries, etc., exist for this sort of
thing?

  Obviously, putting unsigned integers into "network byte order" for
transmission is one such best practice.

  What about signed integers?  Can one expect hton*() and ntoh*() to
work for signed integers as well?  IIRC, most machines store signed
ints in two's-complement format, which I think would survive and work
properly if swapped to compensate for an endianess change, but I'm not
sure.

  What about floating point?

-- Ben


More information about the gnhlug-discuss mailing list