From 6a44d0af2baa97db50250cd8bbd72c1ff25e4f4a Mon Sep 17 00:00:00 2001 From: Lambda Date: Sun, 20 Oct 2024 18:50:05 +0000 Subject: [PATCH] Fix u8_slice_to_hex() For bytes <0x10, this would omit the leading zero. --- src/utils.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index e5d923d5..1eca4b4b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -374,14 +374,14 @@ pub(crate) fn curlify(req: &http::Request) -> Option { /// 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" + ); + } }