Crate hex
Expand description
Hex encoding and decoding.
General purpose hex encoding/decoding library with a conservative MSRV and dependency policy.
§Basic Usage
// In your manifest use the `package` key to improve import ergonomics.
// hex = { package = "hex-conservative", version = "*" }
use hex::prelude::*;
// Decode an arbitrary length hex string into a vector.
let v = Vec::from_hex("deadbeef").expect("valid hex digits");
// Or a known length hex string into a fixed size array.
let a = <[u8; 4]>::from_hex("deadbeef").expect("valid length and valid hex digits");
// We support `LowerHex` and `UpperHex` out of the box for `[u8]` slices.
println!("An array as lower hex: {:x}", a.as_hex());
// And for vecs since `Vec` derefs to byte slice.
println!("A vector as upper hex: {:X}", v.as_hex());
// Allocate a new string (also `to_upper_hex_string`).
let s = v.to_lower_hex_string();
// Please note, mixed case strings will still parse successfully but we only
// support displaying hex in a single case.
assert_eq!(
Vec::from_hex("dEaDbEeF").expect("valid mixed case hex digits"),
Vec::from_hex("deadbeef").expect("valid hex digits"),
);
Modules§
- buf_
encoder - Implements a buffered encoder.
- display
- Helpers for displaying bytes as hex strings.
- error
- Error code for the
hex-conservative
crate. - parse
- Hex encoding and decoding.
- prelude
- Re-exports of the common crate traits.
Macros§
- fmt_
hex_ exact - Format known-length array as hex.
- impl_
fmt_ traits - Adds
core::fmt
trait implementations to type$ty
. - test_
hex_ unwrap - Quick and dirty macro for parsing hex in tests.
- write_
err - Formats error.
Structs§
- Bytes
ToHex Iter - Iterator over bytes which encodes the bytes and yields hex characters.
- HexTo
Bytes Iter - Iterator yielding bytes decoded from an iterator of pairs of hex digits.
- Invalid
Char Error - Invalid hex character.
- OddLength
String Error - Purported hex string had odd length.
Enums§
- Case
- Possible case of hex.
- HexTo
Array Error - Hex decoding error.
- HexTo
Bytes Error - Hex decoding error.
Traits§
- Display
Hex - Extension trait for types that can be displayed as hex.
- FromHex
- Trait for objects that can be deserialized from hex strings.
Type Aliases§
- HexSlice
ToBytes Iter - Convenience alias for
HexToBytesIter<HexDigitsIter<'a>>
.