1pub fn to_vec(hex: &str) -> Option<Vec<u8>> {
2 let mut out = Vec::with_capacity(hex.len() / 2);
3
4 let mut b = 0;
5 for (idx, c) in hex.as_bytes().iter().enumerate() {
6 b <<= 4;
7 match *c {
8 b'A'..=b'F' => b |= c - b'A' + 10,
9 b'a'..=b'f' => b |= c - b'a' + 10,
10 b'0'..=b'9' => b |= c - b'0',
11 _ => return None,
12 }
13 if (idx & 1) == 1 {
14 out.push(b);
15 b = 0;
16 }
17 }
18 Some(out)
19}
20
21#[inline]
22pub fn hex_str(value: &[u8]) -> String {
23 let mut res = String::with_capacity(64);
24 for v in value {
25 res += &format!("{:02x}", v);
26 }
27 res
28}