![]() |
Endian Library |
| Boost Home Endian Home Conversion Functions Endian Types Tutorial |
| Contents |
|
Abstract Introduction to endianness Introduction to the Boost.Endian library Acknowledgements |
| Headers |
|
<boost/endian/conversion.hpp> <boost/endian/types.hpp> |
Boost.Endian provides two facilities to manipulate the byte ordering of integers.
Consider the following code:
int16_t i = 0x0102;
FILE * file = fopen("test.bin", "wb"); // MUST BE BINARY
fwrite(&i, sizeof(int16_t), 1, file);
fclose(file);
On OS X, Linux, or Windows systems with an Intel CPU, a hex dump of the "test.bin" output file produces:
0201
On OS X systems with a PowerPC CPU, or Solaris systems with a SPARC CPU, a hex dump of the "test.bin" 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 a core dump on little-endian systems, most programmers can ignore endianness. But when exchanging binary integers and binary floating point values between computer systems with differing endianness, whether by physical file transfer or over a network, programmers have to deal with endianness in their code.
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 doubleis not currently supported.Endian types - The application uses the provided endian types which mimic the built-in integer types. For example,
big32_torlittle64_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.
Which approach is best for dealing with endianness depends on the application.
| Advantages | |
|---|---|
| Endian types | Endian conversion functions |
|
|
Why bother with endianness?
Binary data portability is the primary use case.
Does endianness have any uses outside of portable binary file or network I/O formats?
Using the unaligned integer types to save internal or external memory space is a minor secondary use case.
Why bother with binary I/O? Why not just use C++ Standard Library stream inserters and extractors?
Binary arithmetic data is smaller and therefore I/O is faster and file sizes are smaller. Transfer between systems is less expensive. Standard interchange formats often specify binary arithmetic data.
Furthermore, binary arithmetic data is of fixed size, and so fixed-size disk records are possible without padding, easing sorting and allowing direct access. Disadvantages, such as the inability to use text utilities on the resulting files, limit usefulness to applications where the binary I/O advantages are paramount.
Why is only big, little, and native endianness supported?
These are the only endian schemes that have any practical value today. PDP-11 and the other middle endian approaches are interesting historical curiosities but have no relevance to C++ developers.
Comments and suggestions were received from Adder, Benaka Moorthi, Christopher Kohlhoff, Cliff Green,Daniel James, Gennaro Proto, Giovanni Piero Deretta, Gordon Woodhull, dizzy, Hartmut Kaiser, Jeff Flinn, John Filo, John Maddock, Kim Barrett, Marsh Ray, Martin Bonner, Mathias Gaunard, Matias Capeletto, Neil Mayhew, Paul Bristow, Phil Endecott, Pyry Jahkola, Rene Rivera, Robert Stewart, Roland Schwarz, Scott McMurray, Sebastian Redl, Tim Blechmann, Tim Moore, tymofey, Tomas Puverle, Vincente Botet, Yuval Ronen and Vitaly Budovski,.
Last revised: 22 May, 2013
© Copyright Beman Dawes, 2011, 2013
Distributed under the Boost Software License, Version 1.0. See www.boost.org/ LICENSE_1_0.txt