Fix u8_slice_to_hex()

For bytes <0x10, this would omit the leading zero.
This commit is contained in:
Lambda 2024-10-20 18:50:05 +00:00
parent 76a633cb66
commit 6a44d0af2b

View file

@ -374,14 +374,14 @@ pub(crate) fn curlify<T>(req: &http::Request<T>) -> Option<String> {
/// whitespace or commas do not appear in the output).
pub(crate) fn u8_slice_to_hex(slice: &[u8]) -> String {
slice.iter().fold(String::new(), |mut acc, x| {
write!(acc, "{x:X}").expect("in-memory write should succeed");
write!(acc, "{x:02X}").expect("in-memory write should succeed");
acc
})
}
#[cfg(test)]
mod tests {
use crate::utils::dbg_truncate_str;
use crate::utils::{dbg_truncate_str, u8_slice_to_hex};
#[test]
fn test_truncate_str() {
@ -395,4 +395,20 @@ mod tests {
assert_eq!(dbg_truncate_str(ok_hand, ok_hand.len() - 1), "👌🏽");
assert_eq!(dbg_truncate_str(ok_hand, ok_hand.len()), "👌🏽");
}
#[test]
fn test_slice_to_hex() {
assert_eq!(u8_slice_to_hex(&[]), "");
assert_eq!(u8_slice_to_hex(&[0]), "00");
assert_eq!(u8_slice_to_hex(&[0xFF]), "FF");
assert_eq!(u8_slice_to_hex(&[1, 2, 3, 4]), "01020304");
assert_eq!(
u8_slice_to_hex(&[0x42; 100]),
"4242424242424242424242424242424242424242\
4242424242424242424242424242424242424242\
4242424242424242424242424242424242424242\
4242424242424242424242424242424242424242\
4242424242424242424242424242424242424242"
);
}
}