blob: f282ab72e0b3cf2fa19db3abb8705caa494ae24f [file] [log] [blame] [view]
Andrew Gallant1c5bfc82015-02-05 18:40:31 -05001This crate provides convenience methods for encoding and decoding numbers in
Andrew Gallant1ee962d2016-12-30 12:12:22 -05002either big-endian or little-endian order.
Andrew Gallantcba50552015-02-03 18:30:01 -05003
Nikolai Vazquezfef20552017-10-29 10:42:27 -04004[![Build status](https://api.travis-ci.org/BurntSushi/byteorder.svg)](https://travis-ci.org/BurntSushi/byteorder)
Andrew Gallante5cb8ba2015-04-16 17:49:32 -04005[![](http://meritbadge.herokuapp.com/byteorder)](https://crates.io/crates/byteorder)
Andrew Gallant1c5bfc82015-02-05 18:40:31 -05006
Andrew Gallanteb4486b2015-04-15 18:30:30 -04007Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org).
Andrew Gallantcba50552015-02-03 18:30:01 -05008
Andrew Gallant5be20452015-02-04 20:57:56 -05009
Andrew Gallant1c5bfc82015-02-05 18:40:31 -050010### Documentation
11
Andrew Gallant1ee962d2016-12-30 12:12:22 -050012https://docs.rs/byteorder
Andrew Gallant5be20452015-02-04 20:57:56 -050013
14
Andrew Gallant1c5bfc82015-02-05 18:40:31 -050015### Installation
16
17This crate works with Cargo and is on
Andrew Gallant1ee962d2016-12-30 12:12:22 -050018[crates.io](https://crates.io/crates/byteorder). Add it to your `Cargo.toml`
19like so:
Andrew Gallant1c5bfc82015-02-05 18:40:31 -050020
21```toml
22[dependencies]
Andrew Gallant1ee962d2016-12-30 12:12:22 -050023byteorder = "1"
Andrew Gallant1c5bfc82015-02-05 18:40:31 -050024```
25
Andrew Gallant3653c8b2015-09-09 22:49:39 -040026If you want to augment existing `Read` and `Write` traits, then import the
Andrew Gallant1c5bfc82015-02-05 18:40:31 -050027extension methods like so:
28
29```rust
30extern crate byteorder;
31
Andrew Poelstra07d85e32015-04-10 13:17:56 -050032use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian, LittleEndian};
Andrew Gallant1c5bfc82015-02-05 18:40:31 -050033```
34
Andrew Gallant1c5bfc82015-02-05 18:40:31 -050035For example:
36
37```rust
Andrew Gallant3653c8b2015-09-09 22:49:39 -040038use std::io::Cursor;
Andrew Poelstra07d85e32015-04-10 13:17:56 -050039use byteorder::{BigEndian, ReadBytesExt};
Andrew Gallant1c5bfc82015-02-05 18:40:31 -050040
Andrew Gallant3653c8b2015-09-09 22:49:39 -040041let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
Andrew Gallant1c5bfc82015-02-05 18:40:31 -050042// Note that we use type parameters to indicate which kind of byte order
43// we want!
44assert_eq!(517, rdr.read_u16::<BigEndian>().unwrap());
45assert_eq!(768, rdr.read_u16::<BigEndian>().unwrap());
46```
Corey Richardson0eeafb22016-05-02 14:39:11 +100047
48### `no_std` crates
49
50This crate has a feature, `std`, that is enabled by default. To use this crate
51in a `no_std` context, add the following to your `Cargo.toml`:
52
53```toml
54[dependencies]
Andrew Gallant1ee962d2016-12-30 12:12:22 -050055byteorder = { version = "1", default-features = false }
Corey Richardson0eeafb22016-05-02 14:39:11 +100056```