use crate::{Result, new_err};
pub fn bytes_to_string(bytes: &[u8]) -> String {
let mut hex_string = String::new();
for elem in bytes {
if elem < &0x10 {
hex_string.push_str(&format!("0{:X}", elem))
} else {
hex_string.push_str(&format!("{:X}", elem))
}
}
hex_string
}
pub fn string_to_bytes(hex_string: &str) -> Result<Vec<u8>> {
const HEX_CHARS: &str = "0123456789ABCDEF";
let mut hex_bytes = vec![0u8; hex_string.len() / 2];
if hex_string.len() % 2 != 0 {
return Err(new_err!(InvalidData: InvalidHex, "Length is not a multiple of 2"));
}
let mut i: usize = 0;
while i < hex_string.len() {
let c1: char = hex_string.chars().nth(i).unwrap();
let c2: char = hex_string.chars().nth(i + 1).unwrap();
if !HEX_CHARS.contains(c1) || !HEX_CHARS.contains(c2) {
return Err(new_err!(InvalidData: InvalidHex, format!("Invalid byte \"{}{}\"", c1, c2)))
}
hex_bytes[i / 2] = u8::from_str_radix(&format!("{}{}", c1, c2), 16).unwrap();
i += 2;
}
Ok(hex_bytes)
}