boost.png (6897 bytes) Endian Library
Boost Home     Endian Home     Conversion Functions     Integer Types     Tutorial
Contents
Abstract
Introduction to endianness
Introduction to the Boost.Endian library
Acknowledgements
Headers
<boost/endian/conversion.hpp>
<boost/endian/integers.hpp>

Abstract

Boost.Endian provides facilities to manipulate the byte ordering of integers.

Introduction to endianness

Consider the following code:

int16_t i = 0x0102;
FILE * file = fopen("test", "wb");   // MUST BE BINARY
fwrite(&i, sizeof(int16_t), 1, file);
fclose(file);

On an Apple, Linux, or Windows computer with an Intel CPU, a hex dump of the "test" output file produces:

0201

On an Apple or other computer with a PowerPC CPU, or an Oracle/Sun Solaris computer with a SPARC CPU, a hex dump of the "test" output file produces:

0102

What's happening here is that Intel CPU's order the bytes of an integer with the least-significant byte first, while SPARC CPU's place the most-significant byte first. Some CPU's, such as the PowerPC, allow the operating system to choose which ordering applies.

Most-significant-byte-first ordering is traditionally called "big endian" ordering and the least-significant-byte-first is traditionally called "little-endian" ordering. The names are derived from Jonathan Swift's satirical novel Gulliver’s Travels, where rival kingdoms opened their soft-boiled eggs at different ends.

See the Wikipedia's Endianness article for an extensive discussion of endianness.

Except for reading an occasional core dump, most programmers can ignore endianness. But when exchanging binary integers with other computer systems, whether by file transfers or over a network, programmers have to deal with endianness.

Introduction to the Boost.Endian library

The Boost.Endian library provides two facilities for dealing with endianness.

The library provides two approaches to dealing with integer endianness:

Endian conversions - The application uses the built-in integer and floating point types, and calls the provided conversion functions to convert byte ordering as needed. Both mutating and non-mutating conversions are supplied, and each comes in unconditional and conditional variants. Type long double is not currently supported.

Endian types - The application uses the provided endian types which mimic the built-in integer types. For example, big32_t or little64_t. Types with lengths of 1-8 bytes are supported, rather than just  2, 4, and 8 bytes. There are no alignment requirements. Floating point types are not currently supported.

Boost Endian is a header-only library.

Choosing between endian types and endian conversion functions

Which approach is best for dealing with endianness depends on the application.

Advantages
Endian types Endian conversion functions
  • Mimic the built-in types. This can simplify use and eliminate logic errors since there is no need to reason about the current endianness of variables.
     
  • 1, 2, 3, 4, 5, 6, 7, and 8 byte integers are supported. Use of 3, 5, 6, or 7 byte integers can reduce internal and external space usage and save I/O time.
     
  • Alignment is not required. This can eliminate padding bytes in structures, reducing internal and external space usage and saving I/O time.
  • Already familiar to developers who have been using C conversion functions for years.
     
  • Uses less CPU time, particularly if each variable is used many times relative to I/O of the variable.
     
  • Easier to pass structures to third-party libraries expecting a specific structure format.
     
  • Supports float and double.

Acknowledgements

Comments and suggestions were received from Benaka Moorthi, Christopher Kohlhoff, Cliff Green, Gennaro Proto, Giovanni Piero Deretta, dizzy, Jeff Flinn, John Maddock, Kim Barrett, Marsh Ray, Martin Bonner, Matias Capeletto, Neil Mayhew, Phil Endecott, Rene Rivera, Robert Stewart, Roland Schwarz, Scott McMurray, Sebastian Redl, Tomas Puverle, Vincente Botet, and Yuval Ronen.


Last revised: 20 May, 2013

© Copyright Beman Dawes, 2011

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