#endian

endianrw

Reading and writing numbers with specific endianness

2 releases

Uses old Rust 2015

0.2.2 Sep 12, 2016
0.2.1 Sep 26, 2015
0.2.0 Sep 24, 2015

#1232 in Rust patterns

MIT license

9KB
133 lines

endianrw

Rust library for reading and writing numbers with specific endianness.

Heavily influenced by byteorder, but with more generic API.

Build Status Crates.io Coveralls.io

License

MIT

Documentation

https://kerhong.github.io/endianrw

Examples

Read

use endianrw::{BigEndian, LittleEndian, EndianReadExt};

let data: Vec<u8> = vec![0x01, 0x23, 0x45, 0x67];

assert_eq!(0x01234567, (&data[..]).read_as::<BigEndian, u32>().unwrap());
assert_eq!(0x67452301, (&data[..]).read_as::<LittleEndian, u32>().unwrap());

Write

use endianrw::{BigEndian, LittleEndian, EndianWriteExt};

let val = 0x01234567;
let mut data: Vec<u8> = vec![0; 4];
(&mut data[..]).write_as::<BigEndian, u32>(val).unwrap();
assert_eq!(0x01, data[0]);

(&mut data[..]).write_as::<LittleEndian, u32>(val).unwrap();
assert_eq!(0x67, data[0]);

No runtime deps