Created
April 22, 2023 11:44
-
-
Save nerodono/b15c0e6d3fb0e9e455213ab063034ba4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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