Crate endianness [−] [src]
This crate provides functions to read numbers from a stream of bytes either in big-endian or little-endian. Functions return Result type instead of panic!.
Examples
Read signed 16-bit integers:
let v = vec![0, 128, 128, 0];
assert_eq!(-32768, read_i16(&v[0..2], ByteOrder::LittleEndian).unwrap());
assert_eq!(-32768, read_i16(&v[2..4], ByteOrder::BigEndian).unwrap());
Read a signed 32-bit integer:
let v = vec![0, 128, 128, 0];
match read_i32(&v, ByteOrder::LittleEndian) {
Ok(n) => println!("Read value {}", n), // 8421376
Err(err) => println!("Error: {}", err),
}
Read a single-precision floating point number:
let v = vec![194, 255, 0, 0];
assert_eq!(-127.5, read_f32(&v[0..4], ByteOrder::BigEndian).unwrap());
Enums
ByteOrder |
The 'ByteOrder' type. It represents the order of bytes in a stream we read from. |
EndiannessError |
The error type. |
Functions
read_f32 |
Reads a single-precision floating point number. |
read_f64 |
Reads a double-precision floating point number. |
read_i16 |
Reads signed 16-bit integer from a stream of bytes. |
read_i32 |
Reads signed 32-bit integer from a stream of bytes. |
read_i64 |
Reads signed 64-bit integer from a stream of bytes. |
read_u16 |
Reads unsigned 16-bit integer from a stream of bytes. |
read_u32 |
Reads unsigned 32-bit integer from a stream of bytes. |
read_u64 |
Reads unsigned 64-bit integer from a stream of bytes. |
Type Definitions
EndiannessResult |
Result type alias that fixes Error parameter. |