#codec #hex #decode #encode #no-std

no-std base16

base16 (hex) encoding and decoding

5 releases

0.2.1 Jul 8, 2019
0.2.0 Jul 7, 2019
0.1.2 Apr 11, 2019
0.1.1 May 2, 2018
0.1.0 May 2, 2018

#891 in Encoding

Download history 14733/week @ 2023-12-13 9635/week @ 2023-12-20 7199/week @ 2023-12-27 12343/week @ 2024-01-03 13186/week @ 2024-01-10 16150/week @ 2024-01-17 18479/week @ 2024-01-24 18662/week @ 2024-01-31 17542/week @ 2024-02-07 20891/week @ 2024-02-14 20265/week @ 2024-02-21 25647/week @ 2024-02-28 17337/week @ 2024-03-06 18217/week @ 2024-03-13 15701/week @ 2024-03-20 12367/week @ 2024-03-27

67,228 downloads per month
Used in 126 crates (48 directly)

CC0 license

28KB
300 lines

base16 (hex) encoding for Rust.

Docs CircleCI codecov

This is a base16 (e.g. hexadecimal) encoding and decoding library which was initially written with an emphasis on performance.

This was before Rust added SIMD, and I haven't gotten around to adding that. It's still probably the fastest non-SIMD impl.

Usage

Add base16 = "0.2" to Cargo.toml, then:

fn main() {
    let original_msg = "Foobar";
    let hex_string = base16::encode_lower(original_msg);
    assert_eq!(hex_string, "466f6f626172");
    let decoded = base16::decode(&hex_string).unwrap();
    assert_eq!(String::from_utf8(decoded).unwrap(), original_msg);
}

More usage examples in the docs.

no_std Usage

This crate supports use in no_std configurations using the following knobs.

  • The "alloc" feature, which is on by default, adds a number of helpful functions that require use of the alloc crate, but not the rest of std. This is no_std compatible.
    • Each function documents if it requires use of the alloc feature.
  • The "std" feature, which is on by default, enables the "alloc" feature, and additionally makes base16::DecodeError implement the std::error::Error trait. (Frustratingly, this trait is in std and not in core or alloc...)

For clarity, this means that by default, we assume you are okay with use of std.

If you'd like to disable the use of std, but are in an environment where you have an allocator (e.g. use of the alloc crate is acceptable), then you require this as alloc-only as follows:

[dependencies]
# Turn of use of `std` (but leave use of `alloc`).
base16 = { version = "0.2", default-features = false, features = ["alloc"] }

If you just want the core base16 functionality and none of the helpers, then you should turn off all features.

[dependencies]
# Turn of use of `std` and `alloc`.
base16 = { version = "0.2", default-features = false }

Both of these configurations are no_std compatible.

License

Public domain, as explained here

No runtime deps