Boost logo

Boost :

From: Caleb Epstein (caleb.epstein_at_[hidden])
Date: 2006-03-28 09:29:30

On 3/28/06, Olaf van der Spek <olafvdspek_at_[hidden]> wrote:

When reading/writing binary streams, you often need to read/write an
> (unaligned) integer in big, little or native endian. Is there support
> for this in Boost?

The closest thing I'm aware of is the portable_binary_oarchive (and
iarchive) in the Boost.Serialization examples directory. This has logic to
save integers and longs in little-endian format regardless of the hardware
architecture, saving a single byte length + binary data that represents the
value. For example, 42 would be saved as \x01\x2a and 1024 would be saved
as \x02\x00\x04

What do other Boost developers/users use/recommend?
> Wouldn't this make a nice additional to the new asio library?

Having machine word conversion logic is indeed very handy for network
programming. The C library will always (?) provide the primitive hton[ls]
and ntoh[ls] routines, but these are not always sufficient in today's
world. Many protocols are strictly little-endian, or utilize
receiver-makes-right semantics, wherein the receiver of the data converts
the data to the correct format for their architecture. General purpose
byte-swapping routines for varying "word" sizes (2, 4, 8, 16) are invaluable
when implementing these sorts of protocols.

Its not entirely clear (to me at least) where this sort of logic belongs in
Boost, as it is potentially useful in more places than just a networking
library. Perhaps this should be just a single header in the boost

Caleb Epstein
caleb dot epstein at gmail dot com

Boost list run by bdawes at, gregod at, cpdaniel at, john at