[][src]Trait byteorder::ByteOrder

pub trait ByteOrder: Clone + Copy + Debug + Default + Eq + Hash + Ord + PartialEq + PartialOrd + Sealed {
    fn read_u16(buf: &[u8]) -> u16;
fn read_u32(buf: &[u8]) -> u32;
fn read_u64(buf: &[u8]) -> u64;
fn read_u128(buf: &[u8]) -> u128;
fn read_uint(buf: &[u8], nbytes: usize) -> u64;
fn read_uint128(buf: &[u8], nbytes: usize) -> u128;
fn write_u16(buf: &mut [u8], n: u16);
fn write_u32(buf: &mut [u8], n: u32);
fn write_u64(buf: &mut [u8], n: u64);
fn write_u128(buf: &mut [u8], n: u128);
fn write_uint(buf: &mut [u8], n: u64, nbytes: usize);
fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize);
fn read_u16_into(src: &[u8], dst: &mut [u16]);
fn read_u32_into(src: &[u8], dst: &mut [u32]);
fn read_u64_into(src: &[u8], dst: &mut [u64]);
fn read_u128_into(src: &[u8], dst: &mut [u128]);
fn write_u16_into(src: &[u16], dst: &mut [u8]);
fn write_u32_into(src: &[u32], dst: &mut [u8]);
fn write_u64_into(src: &[u64], dst: &mut [u8]);
fn write_u128_into(src: &[u128], dst: &mut [u8]);
fn from_slice_u16(numbers: &mut [u16]);
fn from_slice_u32(numbers: &mut [u32]);
fn from_slice_u64(numbers: &mut [u64]);
fn from_slice_u128(numbers: &mut [u128]);
fn from_slice_f32(numbers: &mut [f32]);
fn from_slice_f64(numbers: &mut [f64]); fn read_u24(buf: &[u8]) -> u32 { ... }
fn read_u48(buf: &[u8]) -> u64 { ... }
fn write_u24(buf: &mut [u8], n: u32) { ... }
fn write_u48(buf: &mut [u8], n: u64) { ... }
fn read_i16(buf: &[u8]) -> i16 { ... }
fn read_i24(buf: &[u8]) -> i32 { ... }
fn read_i32(buf: &[u8]) -> i32 { ... }
fn read_i48(buf: &[u8]) -> i64 { ... }
fn read_i64(buf: &[u8]) -> i64 { ... }
fn read_i128(buf: &[u8]) -> i128 { ... }
fn read_int(buf: &[u8], nbytes: usize) -> i64 { ... }
fn read_int128(buf: &[u8], nbytes: usize) -> i128 { ... }
fn read_f32(buf: &[u8]) -> f32 { ... }
fn read_f64(buf: &[u8]) -> f64 { ... }
fn write_i16(buf: &mut [u8], n: i16) { ... }
fn write_i24(buf: &mut [u8], n: i32) { ... }
fn write_i32(buf: &mut [u8], n: i32) { ... }
fn write_i48(buf: &mut [u8], n: i64) { ... }
fn write_i64(buf: &mut [u8], n: i64) { ... }
fn write_i128(buf: &mut [u8], n: i128) { ... }
fn write_int(buf: &mut [u8], n: i64, nbytes: usize) { ... }
fn write_int128(buf: &mut [u8], n: i128, nbytes: usize) { ... }
fn write_f32(buf: &mut [u8], n: f32) { ... }
fn write_f64(buf: &mut [u8], n: f64) { ... }
fn read_i16_into(src: &[u8], dst: &mut [i16]) { ... }
fn read_i32_into(src: &[u8], dst: &mut [i32]) { ... }
fn read_i64_into(src: &[u8], dst: &mut [i64]) { ... }
fn read_i128_into(src: &[u8], dst: &mut [i128]) { ... }
fn read_f32_into(src: &[u8], dst: &mut [f32]) { ... }
fn read_f32_into_unchecked(src: &[u8], dst: &mut [f32]) { ... }
fn read_f64_into(src: &[u8], dst: &mut [f64]) { ... }
fn read_f64_into_unchecked(src: &[u8], dst: &mut [f64]) { ... }
fn write_i16_into(src: &[i16], dst: &mut [u8]) { ... }
fn write_i32_into(src: &[i32], dst: &mut [u8]) { ... }
fn write_i64_into(src: &[i64], dst: &mut [u8]) { ... }
fn write_i128_into(src: &[i128], dst: &mut [u8]) { ... }
fn write_f32_into(src: &[f32], dst: &mut [u8]) { ... }
fn write_f64_into(src: &[f64], dst: &mut [u8]) { ... }
fn from_slice_i16(src: &mut [i16]) { ... }
fn from_slice_i32(src: &mut [i32]) { ... }
fn from_slice_i64(src: &mut [i64]) { ... }
fn from_slice_i128(src: &mut [i128]) { ... } }

ByteOrder describes types that can serialize integers as bytes.

Note that Self does not appear anywhere in this trait's definition! Therefore, in order to use it, you'll need to use syntax like T::read_u16(&[0, 1]) where T implements ByteOrder.

This crate provides two types that implement ByteOrder: BigEndian and LittleEndian. This trait is sealed and cannot be implemented for callers to avoid breaking backwards compatibility when adding new derived traits.

Examples

Write and read u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 4];
LittleEndian::write_u32(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u32(&buf));

Write and read i16 numbers in big endian order:

use byteorder::{ByteOrder, BigEndian};

let mut buf = [0; 2];
BigEndian::write_i16(&mut buf, -5_000);
assert_eq!(-5_000, BigEndian::read_i16(&buf));

Required methods

fn read_u16(buf: &[u8]) -> u16

Reads an unsigned 16 bit integer from buf.

Panics

Panics when buf.len() < 2.

fn read_u32(buf: &[u8]) -> u32

Reads an unsigned 32 bit integer from buf.

Panics

Panics when buf.len() < 4.

Examples

Write and read u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 4];
LittleEndian::write_u32(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u32(&buf));

fn read_u64(buf: &[u8]) -> u64

Reads an unsigned 64 bit integer from buf.

Panics

Panics when buf.len() < 8.

Examples

Write and read u64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 8];
LittleEndian::write_u64(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u64(&buf));

fn read_u128(buf: &[u8]) -> u128

Reads an unsigned 128 bit integer from buf.

Panics

Panics when buf.len() < 16.

Examples

Write and read u128 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 16];
LittleEndian::write_u128(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u128(&buf));

fn read_uint(buf: &[u8], nbytes: usize) -> u64

Reads an unsigned n-bytes integer from buf.

Panics

Panics when nbytes < 1 or nbytes > 8 or buf.len() < nbytes

Examples

Write and read an n-byte number in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_uint(&mut buf, 1_000_000, 3);
assert_eq!(1_000_000, LittleEndian::read_uint(&buf, 3));

fn read_uint128(buf: &[u8], nbytes: usize) -> u128

Reads an unsigned n-bytes integer from buf.

Panics

Panics when nbytes < 1 or nbytes > 16 or buf.len() < nbytes

Examples

Write and read an n-byte number in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_uint128(&mut buf, 1_000_000, 3);
assert_eq!(1_000_000, LittleEndian::read_uint128(&buf, 3));

fn write_u16(buf: &mut [u8], n: u16)

Writes an unsigned 16 bit integer n to buf.

Panics

Panics when buf.len() < 2.

Examples

Write and read u16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 2];
LittleEndian::write_u16(&mut buf, 1_000);
assert_eq!(1_000, LittleEndian::read_u16(&buf));

fn write_u32(buf: &mut [u8], n: u32)

Writes an unsigned 32 bit integer n to buf.

Panics

Panics when buf.len() < 4.

Examples

Write and read u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 4];
LittleEndian::write_u32(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u32(&buf));

fn write_u64(buf: &mut [u8], n: u64)

Writes an unsigned 64 bit integer n to buf.

Panics

Panics when buf.len() < 8.

Examples

Write and read u64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 8];
LittleEndian::write_u64(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u64(&buf));

fn write_u128(buf: &mut [u8], n: u128)

Writes an unsigned 128 bit integer n to buf.

Panics

Panics when buf.len() < 16.

Examples

Write and read u128 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 16];
LittleEndian::write_u128(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u128(&buf));

fn write_uint(buf: &mut [u8], n: u64, nbytes: usize)

Writes an unsigned integer n to buf using only nbytes.

Panics

If n is not representable in nbytes, or if nbytes is > 8, then this method panics.

Examples

Write and read an n-byte number in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_uint(&mut buf, 1_000_000, 3);
assert_eq!(1_000_000, LittleEndian::read_uint(&buf, 3));

fn write_uint128(buf: &mut [u8], n: u128, nbytes: usize)

Writes an unsigned integer n to buf using only nbytes.

Panics

If n is not representable in nbytes, or if nbytes is > 16, then this method panics.

Examples

Write and read an n-byte number in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_uint128(&mut buf, 1_000_000, 3);
assert_eq!(1_000_000, LittleEndian::read_uint128(&buf, 3));

fn read_u16_into(src: &[u8], dst: &mut [u16])

Reads unsigned 16 bit integers from src into dst.

Panics

Panics when src.len() != 2*dst.len().

Examples

Write and read u16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 8];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u16_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u16_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn read_u32_into(src: &[u8], dst: &mut [u32])

Reads unsigned 32 bit integers from src into dst.

Panics

Panics when src.len() != 4*dst.len().

Examples

Write and read u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u32_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn read_u64_into(src: &[u8], dst: &mut [u64])

Reads unsigned 64 bit integers from src into dst.

Panics

Panics when src.len() != 8*dst.len().

Examples

Write and read u64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u64_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn read_u128_into(src: &[u8], dst: &mut [u128])

Reads unsigned 128 bit integers from src into dst.

Panics

Panics when src.len() != 16*dst.len().

Examples

Write and read u128 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 64];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u128_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u128_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn write_u16_into(src: &[u16], dst: &mut [u8])

Writes unsigned 16 bit integers from src into dst.

Panics

Panics when dst.len() != 2*src.len().

Examples

Write and read u16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 8];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u16_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u16_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn write_u32_into(src: &[u32], dst: &mut [u8])

Writes unsigned 32 bit integers from src into dst.

Panics

Panics when dst.len() != 4*src.len().

Examples

Write and read u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u32_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn write_u64_into(src: &[u64], dst: &mut [u8])

Writes unsigned 64 bit integers from src into dst.

Panics

Panics when dst.len() != 8*src.len().

Examples

Write and read u64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u64_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn write_u128_into(src: &[u128], dst: &mut [u8])

Writes unsigned 128 bit integers from src into dst.

Panics

Panics when dst.len() != 16*src.len().

Examples

Write and read u128 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 64];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_u128_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_u128_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn from_slice_u16(numbers: &mut [u16])

Converts the given slice of unsigned 16 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_u16(&mut numbers);
assert_eq!(numbers, [5u16.to_be(), 65000u16.to_be()]);

fn from_slice_u32(numbers: &mut [u32])

Converts the given slice of unsigned 32 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_u32(&mut numbers);
assert_eq!(numbers, [5u32.to_be(), 65000u32.to_be()]);

fn from_slice_u64(numbers: &mut [u64])

Converts the given slice of unsigned 64 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_u64(&mut numbers);
assert_eq!(numbers, [5u64.to_be(), 65000u64.to_be()]);

fn from_slice_u128(numbers: &mut [u128])

Converts the given slice of unsigned 128 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_u128(&mut numbers);
assert_eq!(numbers, [5u128.to_be(), 65000u128.to_be()]);

fn from_slice_f32(numbers: &mut [f32])

Converts the given slice of IEEE754 single-precision (4 bytes) floating point numbers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

fn from_slice_f64(numbers: &mut [f64])

Converts the given slice of IEEE754 double-precision (8 bytes) floating point numbers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Loading content...

Provided methods

fn read_u24(buf: &[u8]) -> u32

Reads an unsigned 24 bit integer from buf, stored in u32.

Panics

Panics when buf.len() < 3.

Examples

Write and read 24 bit u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_u24(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u24(&buf));

fn read_u48(buf: &[u8]) -> u64

Reads an unsigned 48 bit integer from buf, stored in u64.

Panics

Panics when buf.len() < 6.

Examples

Write and read 48 bit u64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 6];
LittleEndian::write_u48(&mut buf, 1_000_000_000_000);
assert_eq!(1_000_000_000_000, LittleEndian::read_u48(&buf));

fn write_u24(buf: &mut [u8], n: u32)

Writes an unsigned 24 bit integer n to buf, stored in u32.

Panics

Panics when buf.len() < 3.

Examples

Write and read 24 bit u32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_u24(&mut buf, 1_000_000);
assert_eq!(1_000_000, LittleEndian::read_u24(&buf));

fn write_u48(buf: &mut [u8], n: u64)

Writes an unsigned 48 bit integer n to buf, stored in u64.

Panics

Panics when buf.len() < 6.

Examples

Write and read 48 bit u64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 6];
LittleEndian::write_u48(&mut buf, 1_000_000_000_000);
assert_eq!(1_000_000_000_000, LittleEndian::read_u48(&buf));

fn read_i16(buf: &[u8]) -> i16

Reads a signed 16 bit integer from buf.

Panics

Panics when buf.len() < 2.

Examples

Write and read i16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 2];
LittleEndian::write_i16(&mut buf, -1_000);
assert_eq!(-1_000, LittleEndian::read_i16(&buf));

fn read_i24(buf: &[u8]) -> i32

Reads a signed 24 bit integer from buf, stored in i32.

Panics

Panics when buf.len() < 3.

Examples

Write and read 24 bit i32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_i24(&mut buf, -1_000_000);
assert_eq!(-1_000_000, LittleEndian::read_i24(&buf));

fn read_i32(buf: &[u8]) -> i32

Reads a signed 32 bit integer from buf.

Panics

Panics when buf.len() < 4.

Examples

Write and read i32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 4];
LittleEndian::write_i32(&mut buf, -1_000_000);
assert_eq!(-1_000_000, LittleEndian::read_i32(&buf));

fn read_i48(buf: &[u8]) -> i64

Reads a signed 48 bit integer from buf, stored in i64.

Panics

Panics when buf.len() < 6.

Examples

Write and read 48 bit i64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 6];
LittleEndian::write_i48(&mut buf, -1_000_000_000_000);
assert_eq!(-1_000_000_000_000, LittleEndian::read_i48(&buf));

fn read_i64(buf: &[u8]) -> i64

Reads a signed 64 bit integer from buf.

Panics

Panics when buf.len() < 8.

Examples

Write and read i64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 8];
LittleEndian::write_i64(&mut buf, -1_000_000_000);
assert_eq!(-1_000_000_000, LittleEndian::read_i64(&buf));

fn read_i128(buf: &[u8]) -> i128

Reads a signed 128 bit integer from buf.

Panics

Panics when buf.len() < 16.

Examples

Write and read i128 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 16];
LittleEndian::write_i128(&mut buf, -1_000_000_000);
assert_eq!(-1_000_000_000, LittleEndian::read_i128(&buf));

fn read_int(buf: &[u8], nbytes: usize) -> i64

Reads a signed n-bytes integer from buf.

Panics

Panics when nbytes < 1 or nbytes > 8 or buf.len() < nbytes

Examples

Write and read n-length signed numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_int(&mut buf, -1_000, 3);
assert_eq!(-1_000, LittleEndian::read_int(&buf, 3));

fn read_int128(buf: &[u8], nbytes: usize) -> i128

Reads a signed n-bytes integer from buf.

Panics

Panics when nbytes < 1 or nbytes > 16 or buf.len() < nbytes

Examples

Write and read n-length signed numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_int128(&mut buf, -1_000, 3);
assert_eq!(-1_000, LittleEndian::read_int128(&buf, 3));

fn read_f32(buf: &[u8]) -> f32

Reads a IEEE754 single-precision (4 bytes) floating point number.

Panics

Panics when buf.len() < 4.

Examples

Write and read f32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let e = 2.71828;
let mut buf = [0; 4];
LittleEndian::write_f32(&mut buf, e);
assert_eq!(e, LittleEndian::read_f32(&buf));

fn read_f64(buf: &[u8]) -> f64

Reads a IEEE754 double-precision (8 bytes) floating point number.

Panics

Panics when buf.len() < 8.

Examples

Write and read f64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let phi = 1.6180339887;
let mut buf = [0; 8];
LittleEndian::write_f64(&mut buf, phi);
assert_eq!(phi, LittleEndian::read_f64(&buf));

fn write_i16(buf: &mut [u8], n: i16)

Writes a signed 16 bit integer n to buf.

Panics

Panics when buf.len() < 2.

Examples

Write and read i16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 2];
LittleEndian::write_i16(&mut buf, -1_000);
assert_eq!(-1_000, LittleEndian::read_i16(&buf));

fn write_i24(buf: &mut [u8], n: i32)

Writes a signed 24 bit integer n to buf, stored in i32.

Panics

Panics when buf.len() < 3.

Examples

Write and read 24 bit i32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_i24(&mut buf, -1_000_000);
assert_eq!(-1_000_000, LittleEndian::read_i24(&buf));

fn write_i32(buf: &mut [u8], n: i32)

Writes a signed 32 bit integer n to buf.

Panics

Panics when buf.len() < 4.

Examples

Write and read i32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 4];
LittleEndian::write_i32(&mut buf, -1_000_000);
assert_eq!(-1_000_000, LittleEndian::read_i32(&buf));

fn write_i48(buf: &mut [u8], n: i64)

Writes a signed 48 bit integer n to buf, stored in i64.

Panics

Panics when buf.len() < 6.

Examples

Write and read 48 bit i64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 6];
LittleEndian::write_i48(&mut buf, -1_000_000_000_000);
assert_eq!(-1_000_000_000_000, LittleEndian::read_i48(&buf));

fn write_i64(buf: &mut [u8], n: i64)

Writes a signed 64 bit integer n to buf.

Panics

Panics when buf.len() < 8.

Examples

Write and read i64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 8];
LittleEndian::write_i64(&mut buf, -1_000_000_000);
assert_eq!(-1_000_000_000, LittleEndian::read_i64(&buf));

fn write_i128(buf: &mut [u8], n: i128)

Writes a signed 128 bit integer n to buf.

Panics

Panics when buf.len() < 16.

Examples

Write and read n-byte i128 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 16];
LittleEndian::write_i128(&mut buf, -1_000_000_000);
assert_eq!(-1_000_000_000, LittleEndian::read_i128(&buf));

fn write_int(buf: &mut [u8], n: i64, nbytes: usize)

Writes a signed integer n to buf using only nbytes.

Panics

If n is not representable in nbytes, or if nbytes is > 8, then this method panics.

Examples

Write and read an n-byte number in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_int(&mut buf, -1_000, 3);
assert_eq!(-1_000, LittleEndian::read_int(&buf, 3));

fn write_int128(buf: &mut [u8], n: i128, nbytes: usize)

Writes a signed integer n to buf using only nbytes.

Panics

If n is not representable in nbytes, or if nbytes is > 16, then this method panics.

Examples

Write and read n-length signed numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut buf = [0; 3];
LittleEndian::write_int128(&mut buf, -1_000, 3);
assert_eq!(-1_000, LittleEndian::read_int128(&buf, 3));

fn write_f32(buf: &mut [u8], n: f32)

Writes a IEEE754 single-precision (4 bytes) floating point number.

Panics

Panics when buf.len() < 4.

Examples

Write and read f32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let e = 2.71828;
let mut buf = [0; 4];
LittleEndian::write_f32(&mut buf, e);
assert_eq!(e, LittleEndian::read_f32(&buf));

fn write_f64(buf: &mut [u8], n: f64)

Writes a IEEE754 double-precision (8 bytes) floating point number.

Panics

Panics when buf.len() < 8.

Examples

Write and read f64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let phi = 1.6180339887;
let mut buf = [0; 8];
LittleEndian::write_f64(&mut buf, phi);
assert_eq!(phi, LittleEndian::read_f64(&buf));

fn read_i16_into(src: &[u8], dst: &mut [i16])

Reads signed 16 bit integers from src to dst.

Panics

Panics when buf.len() != 2*dst.len().

Examples

Write and read i16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 8];
let numbers_given = [1, 2, 0x0f, 0xee];
LittleEndian::write_i16_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i16_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn read_i32_into(src: &[u8], dst: &mut [i32])

Reads signed 32 bit integers from src into dst.

Panics

Panics when src.len() != 4*dst.len().

Examples

Write and read i32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_i32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i32_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn read_i64_into(src: &[u8], dst: &mut [i64])

Reads signed 64 bit integers from src into dst.

Panics

Panics when src.len() != 8*dst.len().

Examples

Write and read i64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_i64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i64_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn read_i128_into(src: &[u8], dst: &mut [i128])

Reads signed 128 bit integers from src into dst.

Panics

Panics when src.len() != 16*dst.len().

Examples

Write and read i128 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 64];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_i128_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i128_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn read_f32_into(src: &[u8], dst: &mut [f32])

Reads IEEE754 single-precision (4 bytes) floating point numbers from src into dst.

Panics

Panics when src.len() != 4*dst.len().

Examples

Write and read f32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1.0, 2.0, 31.312e31, -11.32e19];
LittleEndian::write_f32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0.0; 4];
LittleEndian::read_f32_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn read_f32_into_unchecked(src: &[u8], dst: &mut [f32])

👎 Deprecated since 1.3.0:

please use read_f32_into instead

DEPRECATED.

This method is deprecated. Use read_f32_into instead. Reads IEEE754 single-precision (4 bytes) floating point numbers from src into dst.

Panics

Panics when src.len() != 4*dst.len().

Examples

Write and read f32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1.0, 2.0, 31.312e31, -11.32e19];
LittleEndian::write_f32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0.0; 4];
LittleEndian::read_f32_into_unchecked(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn read_f64_into(src: &[u8], dst: &mut [f64])

Reads IEEE754 single-precision (4 bytes) floating point numbers from src into dst.

Panics

Panics when src.len() != 8*dst.len().

Examples

Write and read f64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1.0, 2.0, 31.312e211, -11.32e91];
LittleEndian::write_f64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0.0; 4];
LittleEndian::read_f64_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn read_f64_into_unchecked(src: &[u8], dst: &mut [f64])

👎 Deprecated since 1.3.0:

please use read_f64_into instead

DEPRECATED.

This method is deprecated. Use read_f64_into instead.

Reads IEEE754 single-precision (4 bytes) floating point numbers from src into dst.

Panics

Panics when src.len() != 8*dst.len().

Examples

Write and read f64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1.0, 2.0, 31.312e211, -11.32e91];
LittleEndian::write_f64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0.0; 4];
LittleEndian::read_f64_into_unchecked(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn write_i16_into(src: &[i16], dst: &mut [u8])

Writes signed 16 bit integers from src into dst.

Panics

Panics when buf.len() != 2*src.len().

Examples

Write and read i16 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 8];
let numbers_given = [1, 2, 0x0f, 0xee];
LittleEndian::write_i16_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i16_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn write_i32_into(src: &[i32], dst: &mut [u8])

Writes signed 32 bit integers from src into dst.

Panics

Panics when dst.len() != 4*src.len().

Examples

Write and read i32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_i32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i32_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn write_i64_into(src: &[i64], dst: &mut [u8])

Writes signed 64 bit integers from src into dst.

Panics

Panics when dst.len() != 8*src.len().

Examples

Write and read i64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_i64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i64_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn write_i128_into(src: &[i128], dst: &mut [u8])

Writes signed 128 bit integers from src into dst.

Panics

Panics when dst.len() != 16*src.len().

Examples

Write and read i128 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 64];
let numbers_given = [1, 2, 0xf00f, 0xffee];
LittleEndian::write_i128_into(&numbers_given, &mut bytes);

let mut numbers_got = [0; 4];
LittleEndian::read_i128_into(&bytes, &mut numbers_got);
assert_eq!(numbers_given, numbers_got);

fn write_f32_into(src: &[f32], dst: &mut [u8])

Writes IEEE754 single-precision (4 bytes) floating point numbers from src into dst.

Panics

Panics when src.len() != 4*dst.len().

Examples

Write and read f32 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 16];
let numbers_given = [1.0, 2.0, 31.312e31, -11.32e19];
LittleEndian::write_f32_into(&numbers_given, &mut bytes);

let mut numbers_got = [0.0; 4];
unsafe {
    LittleEndian::read_f32_into(&bytes, &mut numbers_got);
}
assert_eq!(numbers_given, numbers_got);

fn write_f64_into(src: &[f64], dst: &mut [u8])

Writes IEEE754 double-precision (8 bytes) floating point numbers from src into dst.

Panics

Panics when src.len() != 8*dst.len().

Examples

Write and read f64 numbers in little endian order:

use byteorder::{ByteOrder, LittleEndian};

let mut bytes = [0; 32];
let numbers_given = [1.0, 2.0, 31.312e211, -11.32e91];
LittleEndian::write_f64_into(&numbers_given, &mut bytes);

let mut numbers_got = [0.0; 4];
unsafe {
    LittleEndian::read_f64_into(&bytes, &mut numbers_got);
}
assert_eq!(numbers_given, numbers_got);

fn from_slice_i16(src: &mut [i16])

Converts the given slice of signed 16 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 6500];
BigEndian::from_slice_i16(&mut numbers);
assert_eq!(numbers, [5i16.to_be(), 6500i16.to_be()]);

fn from_slice_i32(src: &mut [i32])

Converts the given slice of signed 32 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_i32(&mut numbers);
assert_eq!(numbers, [5i32.to_be(), 65000i32.to_be()]);

fn from_slice_i64(src: &mut [i64])

Converts the given slice of signed 64 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_i64(&mut numbers);
assert_eq!(numbers, [5i64.to_be(), 65000i64.to_be()]);

fn from_slice_i128(src: &mut [i128])

Converts the given slice of signed 128 bit integers to a particular endianness.

If the endianness matches the endianness of the host platform, then this is a no-op.

Examples

Convert the host platform's endianness to big-endian:

use byteorder::{ByteOrder, BigEndian};

let mut numbers = [5, 65000];
BigEndian::from_slice_i128(&mut numbers);
assert_eq!(numbers, [5i128.to_be(), 65000i128.to_be()]);
Loading content...

Implementors

impl ByteOrder for BigEndian[src]

impl ByteOrder for LittleEndian[src]

Loading content...