boost::endian

Boost introduced a new library endian in version 1.58: Types and conversion functions for correct byte ordering and more regardless of processor endianness.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <boost/endian/conversion.hpp>
 
int32_t x;
 
... read into x from a file ...
 
boost::endian::big_to_native_inplace(x);
 
for (int32_t i = 0; i < 1000000; ++i)
  x += i;
 
boost::endian::native_to_big_inplace(x);
 
... write x to a file ...