
Does Boost have method to do endian swap, like those ntohl and htonl? -- View this message in context: http://www.nabble.com/handle-different-endian-tp20979512p20979512.html Sent from the Boost - Users mailing list archive at Nabble.com.

On Fri, Dec 12, 2008 at 12:02, boost001 <oldyoungguy88@yahoo.com> wrote:
Does Boost have method to do endian swap, like those ntohl and htonl?
Beman Dawes was working on an elegant endian-handling library, but iirc it ran into problems that were only nicely fixable with C++0x features. In the mean time, you could implement an endian swap something like this: #include <boost/detail/endian.hpp> #include <boost/mpl_assert.hpp> #include <boost/type_traits/is_integral.hpp> #include <algorithm> template <typename T> T reverse_bytes(T const x) { BOOST_MPL_ASSERT((boost::is_integral<T>)); T y = x; std::reverse((char*)(&y), (char*)(1+&y)); return y; } #if defined(BOOST_LITTLE_ENDIAN) template <typename T> T hton(T const x) { return reverse_bytes(x); } template <typename T> T ntoh(T const x) { return reverse_bytes(x); } #else template <typename T> T hton(T const x) { return x; } template <typename T> T ntoh(T const x) { return x; } #endif And overload reverse_bytes for anything else you want it to work on.
participants (2)
-
boost001
-
Scott McMurray