torrust_index_backend/utils/
hex.rs

1use std::fmt::Write;
2use std::num::ParseIntError;
3
4#[must_use]
5pub fn from_bytes(bytes: &[u8]) -> String {
6    let mut s = String::with_capacity(2 * bytes.len());
7
8    for byte in bytes {
9        write!(s, "{byte:02X}").unwrap();
10    }
11
12    s
13}
14
15/// Encodes a String into Hex Bytes
16///
17/// # Errors
18///
19/// This function will return an error if unable to encode into Hex
20pub fn into_bytes(s: &str) -> Result<Vec<u8>, ParseIntError> {
21    (0..s.len())
22        .step_by(2)
23        .map(|i| u8::from_str_radix(&s[i..i + 2], 16))
24        .collect()
25}