Giter Site home page Giter Site logo

byte-io-rust's Introduction

byte-io: a simple crate for read/write numbers to/from binary.

This crate only contains 4 functions:

  • write_be: write number to big-endian slice.

  • read_be: read number from big-endian slice.

  • write_le: write number to little-endian slice.

  • read_le: read number from little-endian slice.

Please notice that byte-io does NOT focus on efficiency, which means that it may be slow while handling big streams (e.g. hundreds of Mbytes or more).

How to use

Add the following line to your [dependencies] section in Cargo.toml

byte-io = { git = "https://github.com/zhaihj/byte-io-rust", branch= "master" }

or you can also download it from crates.io:

byte-io = "0.1"

Examples:

Read from a slice is simple:

use byte_io::*;

fn main() {
 let data = [0x00, 0x00, 0x01, 0x01, 0xAB, 0xCD, 0xEF, 0x89];
 assert_eq!(read_be::<u32>(&data), 0x0101);
 assert_eq!(read_be::<u16>(&data[4..]), 0xABCD);
 assert_eq!(read_le::<u16>(&data[4..]), 0xCDAB);
 assert_eq!(read_le::<u8>(&data[4..]), 0xAB);
}

Write is also easy:

use byte_io::*;

fn main() {
 let mut buf = [0u8;8];
 write_be(&0xABCDEFu32, &mut buf);
 assert_eq!(buf, [0x00, 0xAB, 0xCD, 0xEF, 0x00, 0x00, 0x00, 0x00]);
 write_le(&0xABCDEFu32, &mut buf[4..]);
 assert_eq!(buf, [0x00, 0xAB, 0xCD, 0xEF, 0xEF, 0xCD, 0xAB, 0x00]);
}

Moreover, you can even read/write Vec<T>:

use byte_io::*;

fn main() {
 let mut buf = [0u8;8];
 let data = vec![0x1234u16,0x5678u16];
 write_le(&data, &mut buf);
 assert_eq!(buf, [0x34, 0x12, 0x78, 0x56, 0x00, 0x00, 0x00, 0x00]);
 assert_eq!(data, read_le::<Vec<u16>>(&buf[0..4]));
 let u32_vec = read_be::<Vec<u32>>(&buf[4..]);
 assert_eq!(u32_vec.len(), 1);
 assert_eq!(u32_vec.first(), Some(&0));
}

The following code also works:

use byte_io::*;

fn main() {
 let buf = [0xAA, 0xBB, 0xCC, 0xDD];
 assert_eq!(u32::from_u8_be(&buf), 0xAABBCCDD);
}

byte-io-rust's People

Contributors

horasal avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.