Andrew Gallant | 1c5bfc8 | 2015-02-05 18:40:31 -0500 | [diff] [blame] | 1 | This crate provides convenience methods for encoding and decoding numbers in |
Andrew Gallant | 1ee962d | 2016-12-30 12:12:22 -0500 | [diff] [blame] | 2 | either big-endian or little-endian order. |
Andrew Gallant | cba5055 | 2015-02-03 18:30:01 -0500 | [diff] [blame] | 3 | |
Nikolai Vazquez | fef2055 | 2017-10-29 10:42:27 -0400 | [diff] [blame] | 4 | [](https://travis-ci.org/BurntSushi/byteorder) |
Andrew Gallant | e5cb8ba | 2015-04-16 17:49:32 -0400 | [diff] [blame] | 5 | [](https://crates.io/crates/byteorder) |
Andrew Gallant | 1c5bfc8 | 2015-02-05 18:40:31 -0500 | [diff] [blame] | 6 | |
Andrew Gallant | eb4486b | 2015-04-15 18:30:30 -0400 | [diff] [blame] | 7 | Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org). |
Andrew Gallant | cba5055 | 2015-02-03 18:30:01 -0500 | [diff] [blame] | 8 | |
Andrew Gallant | 5be2045 | 2015-02-04 20:57:56 -0500 | [diff] [blame] | 9 | |
Andrew Gallant | 1c5bfc8 | 2015-02-05 18:40:31 -0500 | [diff] [blame] | 10 | ### Documentation |
| 11 | |
Andrew Gallant | 1ee962d | 2016-12-30 12:12:22 -0500 | [diff] [blame] | 12 | https://docs.rs/byteorder |
Andrew Gallant | 5be2045 | 2015-02-04 20:57:56 -0500 | [diff] [blame] | 13 | |
| 14 | |
Andrew Gallant | 1c5bfc8 | 2015-02-05 18:40:31 -0500 | [diff] [blame] | 15 | ### Installation |
| 16 | |
| 17 | This crate works with Cargo and is on |
Andrew Gallant | 1ee962d | 2016-12-30 12:12:22 -0500 | [diff] [blame] | 18 | [crates.io](https://crates.io/crates/byteorder). Add it to your `Cargo.toml` |
| 19 | like so: |
Andrew Gallant | 1c5bfc8 | 2015-02-05 18:40:31 -0500 | [diff] [blame] | 20 | |
| 21 | ```toml |
| 22 | [dependencies] |
Andrew Gallant | 1ee962d | 2016-12-30 12:12:22 -0500 | [diff] [blame] | 23 | byteorder = "1" |
Andrew Gallant | 1c5bfc8 | 2015-02-05 18:40:31 -0500 | [diff] [blame] | 24 | ``` |
| 25 | |
Andrew Gallant | 3653c8b | 2015-09-09 22:49:39 -0400 | [diff] [blame] | 26 | If you want to augment existing `Read` and `Write` traits, then import the |
Andrew Gallant | 1c5bfc8 | 2015-02-05 18:40:31 -0500 | [diff] [blame] | 27 | extension methods like so: |
| 28 | |
| 29 | ```rust |
| 30 | extern crate byteorder; |
| 31 | |
Andrew Poelstra | 07d85e3 | 2015-04-10 13:17:56 -0500 | [diff] [blame] | 32 | use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian, LittleEndian}; |
Andrew Gallant | 1c5bfc8 | 2015-02-05 18:40:31 -0500 | [diff] [blame] | 33 | ``` |
| 34 | |
Andrew Gallant | 1c5bfc8 | 2015-02-05 18:40:31 -0500 | [diff] [blame] | 35 | For example: |
| 36 | |
| 37 | ```rust |
Andrew Gallant | 3653c8b | 2015-09-09 22:49:39 -0400 | [diff] [blame] | 38 | use std::io::Cursor; |
Andrew Poelstra | 07d85e3 | 2015-04-10 13:17:56 -0500 | [diff] [blame] | 39 | use byteorder::{BigEndian, ReadBytesExt}; |
Andrew Gallant | 1c5bfc8 | 2015-02-05 18:40:31 -0500 | [diff] [blame] | 40 | |
Andrew Gallant | 3653c8b | 2015-09-09 22:49:39 -0400 | [diff] [blame] | 41 | let mut rdr = Cursor::new(vec![2, 5, 3, 0]); |
Andrew Gallant | 1c5bfc8 | 2015-02-05 18:40:31 -0500 | [diff] [blame] | 42 | // Note that we use type parameters to indicate which kind of byte order |
| 43 | // we want! |
| 44 | assert_eq!(517, rdr.read_u16::<BigEndian>().unwrap()); |
| 45 | assert_eq!(768, rdr.read_u16::<BigEndian>().unwrap()); |
| 46 | ``` |
Corey Richardson | 0eeafb2 | 2016-05-02 14:39:11 +1000 | [diff] [blame] | 47 | |
| 48 | ### `no_std` crates |
| 49 | |
| 50 | This crate has a feature, `std`, that is enabled by default. To use this crate |
| 51 | in a `no_std` context, add the following to your `Cargo.toml`: |
| 52 | |
| 53 | ```toml |
| 54 | [dependencies] |
Andrew Gallant | 1ee962d | 2016-12-30 12:12:22 -0500 | [diff] [blame] | 55 | byteorder = { version = "1", default-features = false } |
Corey Richardson | 0eeafb2 | 2016-05-02 14:39:11 +1000 | [diff] [blame] | 56 | ``` |