Skip to content

Instantly share code, notes, and snippets.

@niksaak
Forked from anonymous/playground.rs
Created December 12, 2017 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niksaak/91f176af470093bf3b15c9e5af5ba466 to your computer and use it in GitHub Desktop.
Save niksaak/91f176af470093bf3b15c9e5af5ba466 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
use std::str;
fn main() {
let b = "67e5504410b1426f9247bb680e5fe0c8";
let mut bytes = [0u8; 16];
for (dest, chunk) in bytes.iter_mut().zip(b.as_bytes().chunks(2)) {
let s = match str::from_utf8(chunk) {
Ok(v) => v,
Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
};
match u8::from_str_radix(s, 16) {
Ok(byte) => *dest = byte,
Err(..) => {},
}
}
println!("{:?}", bytes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment