1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use super::ByteFormat;
use std::fmt;
pub const DEFAULT_HEX: FormatHex<'static> = FormatHex {
prefix: "",
separator: " ",
uppercase: true,
};
#[derive(Copy, Clone, Debug)]
pub struct FormatHex<'s> {
pub prefix: &'s str,
pub separator: &'s str,
pub uppercase: bool,
}
impl<'s> ByteFormat for FormatHex<'s> {
fn fmt_bytes(&self, bytes: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
let mut written = false;
for b in bytes {
if written {
f.write_str(self.separator)?;
}
if self.uppercase {
write!(f, "{}{:02X}", self.prefix, b)?;
} else {
write!(f, "{}{:02x}", self.prefix, b)?;
}
written = true;
}
Ok(())
}
}
#[test]
fn test_hex() {
assert_eq!(DEFAULT_HEX.bytes_to_string(b"\xAB\xCD\xEF\x00\x10"), "AB CD EF 00 10");
}