Skip to main content

decode_to_array

Function decode_to_array 

Source
pub fn decode_to_array<const N: usize>(
    hex: &str,
) -> Result<[u8; N], DecodeFixedLengthBytesError>
Expand description

Decodes a hex string with an expected length known at compile time.

If you don’t know the required length at compile time you need to use decode_to_vec instead.

§Errors

Returns an error if hex contains invalid characters or has incorrect length. (Should be N * 2.)

Examples found in repository?
examples/hexy.rs (line 55)
53    fn from_str(s: &str) -> Result<Self, Self::Err> {
54        // Errors if the input is invalid
55        let a = hex::decode_to_array::<32>(s)?;
56        Ok(Hexy { data: a })
57    }
More examples
Hide additional examples
examples/wrap_array.rs (line 16)
12fn main() {
13    let hex = "deadbeefcafebabedeadbeefcafebabedeadbeefcafebabedeadbeefcafebabe";
14    println!("\nParse from hex: {}\n", hex);
15
16    let array = hex::decode_to_array::<32>(hex).expect("failed to parse array");
17    let wrap = Wrap::from_str(hex).expect("failed to parse wrapped array from hex string");
18
19    println!("Print an array using traits from the standard libraries `fmt` module along with the provided implementation of `DisplayHex`:\n");
20    println!("LowerHex: {:x}", array.as_hex());
21    println!("UpperHex: {:X}", array.as_hex());
22    println!("Display: {}", array.as_hex());
23    println!("Debug: {:?}", array.as_hex());
24    println!("Debug pretty: {:#?}", array.as_hex());
25
26    println!("\n");
27
28    println!(
29        "Print the wrapped array directly using traits from the standard libraries `fmt` module:\n"
30    );
31    println!("LowerHex: {:x}", wrap);
32    println!("UpperHex: {:X}", wrap);
33    println!("Display: {}", wrap);
34    println!("Debug: {:?}", wrap);
35    println!("Debug pretty: {:#?}", wrap);
36
37    #[cfg(feature = "alloc")]
38    {
39        let array_hex = array.to_lower_hex_string();
40        let other = array.as_hex().to_string();
41        assert_eq!(array_hex, other);
42
43        let wrap_hex = wrap.to_string();
44        assert_eq!(array_hex, wrap_hex);
45    }
46}
47
48pub struct Wrap([u8; 32]);
49
50impl fmt::Debug for Wrap {
51    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
52        fmt::Formatter::debug_tuple(f, "Wrap").field(&self.0.as_hex()).finish()
53    }
54}
55
56impl fmt::Display for Wrap {
57    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
58        fmt::Display::fmt(&self.0.as_hex(), f)
59    }
60}
61
62impl fmt::LowerHex for Wrap {
63    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
64        fmt::LowerHex::fmt(&self.0.as_hex(), f)
65    }
66}
67
68impl fmt::UpperHex for Wrap {
69    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
70        fmt::LowerHex::fmt(&self.0.as_hex(), f)
71    }
72}
73
74impl FromStr for Wrap {
75    type Err = DecodeFixedLengthBytesError;
76    fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(Self(hex::decode_to_array::<32>(s)?)) }