Skip to content

Instantly share code, notes, and snippets.

@nerodono
Created April 22, 2023 11:44
Show Gist options
  • Save nerodono/b15c0e6d3fb0e9e455213ab063034ba4 to your computer and use it in GitHub Desktop.
Save nerodono/b15c0e6d3fb0e9e455213ab063034ba4 to your computer and use it in GitHub Desktop.
pub(crate) fn parse_hex(buffer: &[u8]) -> Result<usize, ()> {
let mut output = 0;
let buf_len = buffer.len();
for (idx, chr) in buffer.iter().copied().enumerate() {
let shl = 4 * (buf_len - idx - 1);
match chr {
alpha @ b'a'..=b'f' => {
output += (10_usize + (alpha - b'a') as usize) << shl;
}
digit @ b'0'..=b'9' => {
output += ((digit - b'0') as usize) << shl;
}
_ => return Err(()),
}
}
Ok(output)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment