boost.png (6897 bytes) Endian Conversion Functions
Boost Home     Endian Home     Conversion Functions     Arithmetic Types     Buffer Types

Contents
Introduction
Reference
    Synopsis
    Requirements
      Reversible
      Customization for UDTs
Functions
FAQ
Acknowledgements
Headers
<boost/endian/conversion.hpp>
<boost/endian/buffers.hpp>
<boost/endian/arithmetic.hpp>

Introduction

Header boost/endian/conversion.hpp provides byte order reversal and conversion functions that convert objects of the built-in integer types, and also types float and double, between native, big, or little endian byte ordering. User defined types are also supported.

Reference

Functions are implemented if appropriate. noexcept is elided for compilers that do not support it. Boost scoped enum emulation is used so that the library still works for compilers that do not support scoped enums.

Header <boost/endian/conversion.hpp> Synopsis

#define BOOST_ENDIAN_INTRINSIC_MSG "message describing presence or absence of intrinsics"

namespace boost
{
namespace endian
{
  enum class order
  {
    big,                             // big-endian
    little,                          // little-endian
    native = implementation-defined  // same as order::big or order::little
  };

  int8_t   endian_reverse(int8_t x) noexcept;
  int16_t  endian_reverse(int16_t x) noexcept;
  int32_t  endian_reverse(int32_t x) noexcept;
  int64_t  endian_reverse(int64_t x) noexcept;
  uint8_t  endian_reverse(uint8_t x) noexcept;
  uint16_t endian_reverse(uint16_t x) noexcept;
  uint32_t endian_reverse(uint32_t x) noexcept;
  uint64_t endian_reverse(uint64_t x) noexcept;
  float    endian_reverse(float x) noexcept;
  double   endian_reverse(double x) noexcept;

  template <class Reversible>
    Reversible big_to_native(Reversible x) noexcept;
  template <class Reversible>
    Reversible native_to_big(Reversible x) noexcept;
  template <class Reversible>
    Reversible little_to_native(Reversible x) noexcept;
  template <class Reversible>
    Reversible native_to_little(Reversible x) noexcept;
  template <order O1, order O2, class Reversible>
    Reversible conditional_reverse(Reversible x) noexcept;
  template <class Reversible>
    Reversible conditional_reverse(Reversible x,
      order order1, order order2) noexcept;
      
  template <class Reversible>
    void endian_reverse_in_place(Reversible& x) noexcept;

  template <class Reversible>
    void big_to_native_in_place(Reversible& x) noexcept;
  template <class Reversible>
    void native_to_big_in_place(Reversible& x) noexcept;
  template <class Reversible>
    void little_to_native_in_place(Reversible& x) noexcept;
  template <class Reversible>
    void native_to_little_in_place(Reversible& x) noexcept;
  template <order O1, order O2, class Reversible>
    void conditional_reverse_in_place(Reversible& x) noexcept; 
  template <class Reversible>
   void conditional_reverse_in_place(Reversible& x,
     order order1, order order2) noexcept;

} // namespace endian
} // namespace boost

The implementation is required to define the enum class order constant native as big on big endian platforms and little on little endian platforms.

Requirements

Template argument Reversible requirements

This subsection describes names that are used to specify constraints on template arguments.

User-defined types may be used in the function templates in this header only if they meet these requirements.

Reversible is an object type to be supplied by a C++ program instantiating a template.

Expression Meaning of x Requirements

endian_reverse(x)

x is a value of a possibly const type convertible to Reversible.

Returns the value of x with the order of its constituent bytes reversed.

endian_reverse_in_place(x) x is a modifiable lvalue of type Reversible. The order of the constituent bytes of x are reversed.

[Note: A user-defined type meets these requirements by defining a non-member function in the same namespace as the UDT itself so that the function can be found by argument dependent lookup (ADL).  —end note]

[Note: Because there is a function template for endian_reverse_in_place that calls endian_reverse, only endian_reverse is required for a user-defined type to meet the Reversible requirements. User-defined types may provide endian_reverse_in_place for improved efficiency. —end note]

This still isn't right. There are really separate Reversible and ReversibleHere requirements, and the endian_reverse_here template's type should be Reversible. See the udt_conversion_example for a type that is ReversibleHere but not Reversible and works just fine.

Customization points for user-defined types (UDTs)

This subsection describes requirements on the endian library's own implementation.

The endianness reversal function templates that return values are required to perform reversal of endianness if needed by making an unqualified call to endian_reverse(argument), as described in the preceding table.

The endianness reversal function templates that modify their argument in place, except endian_reverse_in_place itself, are required to perform reversal of endianness if needed by making an unqualified call to endian_reverse_in_place(argument), as described in the preceding table.

See udt_conversion_example.cpp for an example user-defined type.

Functions

int8_t   endian_reverse(int8_t x) noexcept;
int16_t  endian_reverse(int16_t x) noexcept;
int32_t  endian_reverse(int32_t x) noexcept;
int64_t  endian_reverse(int64_t x) noexcept;
uint8_t  endian_reverse(uint8_t x) noexcept;
uint16_t endian_reverse(uint16_t x) noexcept;
uint32_t endian_reverse(uint32_t x) noexcept;
uint64_t endian_reverse(uint64_t x) noexcept;
float    endian_reverse(float x) noexcept;
double   endian_reverse(double x) noexcept;

Returns: x, with the order of its constituent bytes reversed.

template <class Reversible>
Reversible big_to_native(Reversible x) noexcept;

Returns: conditional_reverse<order::big, order::native>(x).

template <class Reversible>
Reversible native_to_big(Reversible x) noexcept; 

Returns: conditional_reverse<order::native, order::big>(x).

template <class Reversible>
Reversible little_to_native(Reversible x) noexcept; 

Returns: conditional_reverse<order::little, order::native>(x).

template <class Reversible>
Reversible native_to_little(Reversible x) noexcept; 

Returns: conditional_reverse<order::native, order::little>(x).

template <order O1, order O2, class Reversible>
Reversible conditional_reverse(Reversible x) noexcept; 

Returns: x if O1 == O2, otherwise endian_reverse(x).

Remarks: Whether x or endian_reverse(x) is to be returned shall be determined at compile time.

template <class Reversible>
Reversible conditional_reverse(Reversible x,
      order order1, order order2) noexcept; 

Returns: order1 == order2 ? x : endian_reverse(x).

template <class Reversible>
void endian_reverse_in_place(Reversible& x) noexcept; 

Effects: x = endian_reverse(x).

template <class Reversible>
void big_to_native_in_place(Reversible& x) noexcept; 

Effects: conditional_reverse_in_place<order::big, order::native>(x).

template <class Reversible>
void native_to_big_in_place(Reversible& x) noexcept; 

Effects: conditional_reverse_in_place<order::native, order::big>(x).

template <class Reversible>
void little_to_native_in_place(Reversible& x) noexcept; 

Effects: conditional_reverse_in_place<order::little, order::native>(x).

template <class Reversible>
void native_to_little_in_place(Reversible& x) noexcept; 

Effects: conditional_reverse_in_place<order::native, order::little>(x).

template <order O1, order O2, class Reversible>
void conditional_reverse_in_place(Reversible& x) noexcept; 

Effects: None if O1 == O2, otherwise endian_reverse_in_place(x).

Remarks: Which effect applies shall be determined at compile time.

template <class Reversible>
void conditional_reverse_in_place(Reversible& x,
     order order1, order order2) noexcept; 

Effects: If order1 == order2 then endian_reverse_in_place(x).

FAQ

See the Endian home page FAQ for a library-wide FAQ.

Why are the template versions of reverse() and endian_reverse() in a detail namespace?

They are unsafe for general use. Consider reversing the bytes of a std::pair as a whole - the bytes from first would end up in second and visa versa, and this is totally wrong!

Why are both value returning and modify-in-place functions provided?

Returning the result by value is the standard C and C++ idiom for functions that compute a value from an argument. Modify-in-place functions allow cleaner code in many real-world endian use cases and are more efficient for user-defined types that have members such as string data that do not need to be reversed. Thus both forms are provided.

Acknowledgements

Tomas Puverle was instrumental in identifying and articulating the need to support endian conversion as separate from endian integer types. Phil Endecott suggested the form of the value returning signatures. Vicente Botet and other reviewers suggested supporting floating point types and user defined types. General reverse template implementation approach using std::reverse suggested by Mathias Gaunard. Portable implementation approach for 16, 32, and 64-bit integers suggested by tymofey, with avoidance of undefined behavior as suggested by Giovanni Piero Deretta, and a further refinement suggested by Pyry Jahkola. Intrinsic builtins implementation approach for 16, 32, and 64-bit integers suggested by several reviewers, and by David Stone, who provided his Boost licensed macro implementation that became the starting point for boost/endian/detail/intrinsic.hpp. Pierre Talbot provided the int8_t endian_reverse() and templated reverse() implementations.


Last revised: 10 December, 2014

© Copyright Beman Dawes, 2011, 2013

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