Hex string to Vec<u8>

Is there a crate for taking a string of hex specified by the user and break it out into a Vec array?

For example:
given input of string "0a1b2c3d4e5f" convert it to [0, a, 1, b, 2, c, 3, d, 4, e, 5, f]

It is trivial using the standard library (playground):

fn hex_to_bytes(s: &str) -> Option<Vec<u8>> {
    if s.len() % 2 == 0 {
        (0..s.len())
            .step_by(2)
            .map(|i| s.get(i..i + 2)
                      .and_then(|sub| u8::from_str_radix(sub, 16).ok()))
            .collect()
    } else {
        None
    }
}
2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.