use std::error::Error;
pub fn hex_str_to_bytes(s: &str) -> Result<Vec<u8>, HexError> {
for (i, ch) in s.chars().enumerate() {
if !ch.is_digit(16) {
return Err(HexError(format!("invalid character position {}", i)));
}
}
let input: Vec<_> = s.chars().collect();
let decoded: Vec<u8> = input
.chunks(2)
.map(|chunk| {
((chunk[0].to_digit(16).unwrap() << 4) | (chunk[1].to_digit(16).unwrap())) as u8
})
.collect();
Ok(decoded)
}
pub fn bytes_to_hex_str(b: &[u8]) -> String {
b.iter()
.map(|b| format!("{:02x}", b))
.collect::<Vec<_>>()
.join("")
}
#[derive(Debug)]
pub struct HexError(pub String);
impl Error for HexError {}
impl std::fmt::Display for HexError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(&self.0)
}
}