boost.png (6897 bytes) Endian Conversion Functions
Boost Home     Endian Home     Conversion Functions     Integer Types     Tutorial
Contents
Introduction
Reference
    Synopsis
    Members
Acknowledgements
Headers
<boost/endian/conversion.hpp>
<boost/endian/integers.hpp>

Introduction

Header boost/endian/conversion.hpp provides functions that convert built-in integers between native byte ordering and big or little endian byte ordering.

Reference

Synopsis

namespace boost
{
namespace endian
{
  // unconditional modifying (i.e. in-place) reverse byte order

  inline void reorder(int16_t& x);
  inline void reorder(int32_t& x);
  inline void reorder(int64_t& x);
  inline void reorder(uint16_t& x);
  inline void reorder(uint32_t& x);
  inline void reorder(uint64_t& x);

  // unconditional non-modifying reverse byte order copy

  inline void reorder(int16_t source, int16_t& target);
  inline void reorder(int32_t source, int32_t& target);
  inline void reorder(int64_t source, int64_t& target);
  inline void reorder(uint16_t source, uint16_t& target);
  inline void reorder(uint32_t source, uint32_t& target);
  inline void reorder(uint64_t source, uint64_t& target);

  // conditional modifying (i.e. in-place) reverse byte order

  template <class T> void native_to_big(T& x);
  template <class T> void native_to_little(T& x);
  template <class T> void big_to_native(T& x);
  template <class T> void little_to_native(T& x);

  // non-modifying conditional reverse byte order copy

  template <class T> void native_to_big(T source, T& target);
  template <class T> void native_to_little(T source, T& target);
  template <class T> void big_to_native(T source, T& target);
  template <class T> void little_to_native(T source, T& target);

} // namespace endian
} // namespace boost

Members

inline void reorder(int16_t& x);
inline void reorder(int32_t& x);
inline void reorder(int64_t& x);
inline void reorder(uint16_t& x);
inline void reorder(uint32_t& x);
inline void reorder(uint64_t& x);

Effects: Reverses the byte order of x.

inline void reorder(int16_t source, int16_t& target);
inline void reorder(int32_t source, int32_t& target);
inline void reorder(int64_t source, int64_t& target);
inline void reorder(uint16_t source, uint16_t& target);
inline void reorder(uint32_t source, uint32_t& target);
inline void reorder(uint64_t source, uint64_t& target);

Effects: Copies source to target, reversing the byte order.

template <class T> void native_to_big(T& x);
template <class T> void native_to_little(T& x);
template <class T> void big_to_native(T& x);
template <class T> void little_to_native(T& x);

Effects: If the native byte ordering and byte ordering indicated by the function name are different, reorder(x), otherwise no effect.

Example:

int32_t x = some-value;
native_to_big(x); // converts x to big-endian unless
                  // the native representation is already big-endian
template <class T> void native_to_big(T source, T& target);
template <class T> void native_to_little(T source, T& target);
template <class T> void big_to_native(T source, T& target);
template <class T> void little_to_native(T source, T& target);

Effects: If the native byte ordering and byte ordering indicated by the function name are different, reorder(source, target), otherwise target = source.

Example:

int32_t x;
... read an external little-endian value into x ...
int32_t y;
little_to_native(x, y); // if native ordering is big-endian, reorder(x, y),
                        // otherwise y = x

Acknowledgements

Tomas Puverle was instrumental in identifying and articulating the need to support endian conversion as separate from endian types.


Last revised: 05 September, 2011

© Copyright Beman Dawes, 2011

Distributed under the Boost Software License, Version 1.0. See www.boost.org/ LICENSE_1_0.txt