#numbers #endian #reading #byte #integer #read #16-bit

endianness

Rust library for reading numbers in big-endian and little-endian

2 unstable releases

Uses old Rust 2015

0.2.0 Mar 24, 2016
0.1.0 Feb 28, 2016

#19 in #16-bit

Download history 62/week @ 2024-07-21 95/week @ 2024-07-28 180/week @ 2024-08-04 160/week @ 2024-08-11 149/week @ 2024-08-18 190/week @ 2024-08-25 84/week @ 2024-09-01 167/week @ 2024-09-08 259/week @ 2024-09-15 197/week @ 2024-09-22 202/week @ 2024-09-29 185/week @ 2024-10-06 174/week @ 2024-10-13 201/week @ 2024-10-20 136/week @ 2024-10-27 79/week @ 2024-11-03

611 downloads per month
Used in kafka-serde

MIT license

12KB
182 lines

rust-endianness

Library provides functions to read numbers from a stream of bytes either in big-endian or little-endian. Functions return Result type.

Build Status

Installation

Add the package to your Cargo.toml:

[dependencies]
endianness = "0.2"

and then import it:

extern crate endianness;
use endianness::*;

Usage

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());

No runtime deps