GithubHelp home page GithubHelp logo

cisen / sourcecode-byteorder-00 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from burntsushi/byteorder

0.0 0.0 0.0 277 KB

Rust library for reading/writing numbers in big-endian and little-endian.

License: The Unlicense

Rust 99.99% Shell 0.01%

sourcecode-byteorder-00's Introduction

总结

api 说明

  • read_u16深拷贝读取两个字节并返回那两个字节的数据,并且会清空旧的对应内存的数据
use std::io::Cursor;
use byteorder::{BigEndian, ReadBytesExt};
// 默认时u8无符号整型,所以对应的数字时
// [00000010, 00000101, 00000011, 00000000]
let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
// Note that we use type parameters to indicate which kind of byte order
// we want!
// 读取头部的16位则问 0000001000000101 = 2^9 + 2^2 + 2^0 = 512 + 4 + 1 = 517
// 读取完删除已读取的内存
assert_eq!(517, rdr.read_u16::<BigEndian>().unwrap());
// 读取剩下的为: 0000001100000000 = 2^9 + 2^8 = 512 + 256 = 768
assert_eq!(768, rdr.read_u16::<BigEndian>().unwrap());

byteorder

This crate provides convenience methods for encoding and decoding numbers in either big-endian or little-endian order.

Build status

Dual-licensed under MIT or the UNLICENSE.

Documentation

https://docs.rs/byteorder

Installation

This crate works with Cargo and is on crates.io. Add it to your Cargo.toml like so:

[dependencies]
byteorder = "1"

If you want to augment existing Read and Write traits, then import the extension methods like so:

use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian, LittleEndian};

For example:

use std::io::Cursor;
use byteorder::{BigEndian, ReadBytesExt};

let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
// Note that we use type parameters to indicate which kind of byte order
// we want!
assert_eq!(517, rdr.read_u16::<BigEndian>().unwrap());
assert_eq!(768, rdr.read_u16::<BigEndian>().unwrap());

no_std crates

This crate has a feature, std, that is enabled by default. To use this crate in a no_std context, add the following to your Cargo.toml:

[dependencies]
byteorder = { version = "1", default-features = false }

Alternatives

Note that as of Rust 1.32, the standard numeric types provide built-in methods like to_le_bytes and from_le_bytes, which support some of the same use cases.

sourcecode-byteorder-00's People

Contributors

andersk avatar andydude avatar apoelstra avatar aschampion avatar blackbeam avatar brson avatar burntsushi avatar cesarb avatar cisen avatar codyps avatar cuviper avatar dtulig avatar emberian avatar enet4 avatar fenhl avatar fpgaminer avatar gankra avatar geofft avatar guillaumegomez avatar huonw avatar ignatenkobrain avatar jturner314 avatar lambda avatar lukesteensen avatar newpavlov avatar oconnor663 avatar samwhited avatar sdroege avatar tspiteri avatar waywardmonkeys 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.