Expand description

This crate provides convenience methods for encoding and decoding numbers in either big-endian or little-endian order.

The organization of the crate is pretty simple. A trait, ByteOrder, specifies byte conversion methods for each type of number in Rust (sans numbers that have a platform dependent size like usize and isize). Two types, BigEndian and LittleEndian implement these methods. Finally, ReadBytesExt and WriteBytesExt provide convenience methods available to all types that implement Read and Write.

An alias, NetworkEndian, for BigEndian is provided to help improve code clarity.

An additional alias, NativeEndian, is provided for the endianness of the local platform. This is convenient when serializing data for use and conversions are not desired.


Read unsigned 16 bit big-endian integers from a Read type:

use std::io::Cursor;
use byteorder::{BigEndian, ReadBytesExt};

let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
// Note that we use type parameters to indicate which kind of byte order
// we want!
assert_eq!(517, rdr.read_u16::<BigEndian>().unwrap());
assert_eq!(768, rdr.read_u16::<BigEndian>().unwrap());

Write unsigned 16 bit little-endian integers to a Write type:

use byteorder::{LittleEndian, WriteBytesExt};

let mut wtr = vec![];
assert_eq!(wtr, vec![5, 2, 0, 3]);

Optional Features

This crate optionally provides support for 128 bit values (i128 and u128) when built with the i128 feature enabled.

This crate can also be used without the standard library.


Note that as of Rust 1.32, the standard numeric types provide built-in methods like to_le_bytes and from_le_bytes, which support some of the same use cases.


io 🔒
private 🔒


Copies a &u8 $src into a &mut [] $dst for the endianness given by $which (must be either to_be or to_le).

Copies $size bytes from a number $n to a &mut u8 $dst. $ty represents the numeric type of $n and $which must be either to_be or to_le, depending on which endianness one wants to use when writing to $dst.

Copies a &[$ty] $src into a &mut u8 $dst, where $ty must be a numeric type. This panics if size_of::<$ty>() * $src.len() != $dst.len().


Defines big-endian serialization.

Defines little-endian serialization.


ByteOrder describes types that can serialize integers as bytes.

Extends Read with methods for reading numbers. (For std::io.)

Extends Write with methods for writing numbers. (For std::io.)


Type Definitions

A type alias for BigEndian.

A type alias for LittleEndian.

Defines system native-endian serialization.

Defines network byte order serialization.