Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2008-01-18 08:25:21


Richard:

> Does boost have a facility for this?
>
> I have a bunch of code that did binary I/O on x86. I want to make the
> code endian-neutral. This is similar to writing binary data in
> "network order" as is done with TCP and other internet protocols.
>
> I was thinking that you could now determine completely at compile-time
> whether or not bytes need to be swapped using a template class.
>
> My thought was that you would have a class for writing "LittleEndian"
> bytes that took a boolean template parameter. [...]

I remember a Boost.Endian library being discussed some time ago, but I don't
know what happened to it.

Endianness libraries aside, here's what I do:

void write( unsigned v )
{
    // external representation is 32 bit LE

    write( (unsigned char)( v & 0xFF ) );
    write( (unsigned char)( ( v >> 8 ) & 0xFF ) );
    write( (unsigned char)( ( v >> 16 ) & 0xFF ) );
    write( (unsigned char)( ( v >> 24 ) & 0xFF ) );
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net