use std::fmt::Write;
pub fn to_hex(bytes: &[u8]) -> String {
let mut result = String::with_capacity(bytes.len() * 2);
bytes.iter().for_each(|b| {
write!(result, "{b:02x}").unwrap();
});
result
}
pub fn opt_from_hex(hex: &str) -> Option<Vec<u8>> {
fn hex_to_decimal(hex_digit: u8) -> Option<u8> {
Some(match hex_digit {
b'0'..=b'9' => hex_digit - b'0',
b'a'..=b'f' => hex_digit - b'a' + 0xa,
b'A'..=b'F' => hex_digit - b'A' + 0xa,
_ => return None,
})
}
if hex.len() % 2 != 0 {
return None;
}
hex.as_bytes()
.chunks_exact(2)
.map(
|slice| match (hex_to_decimal(slice[0]), hex_to_decimal(slice[1])) {
(Some(h), Some(l)) => Some(h * 0x10 + l),
_ => None,
},
)
.collect()
}
pub fn from_hex(hex: &str) -> Vec<u8> {
opt_from_hex(hex).unwrap()
}