pub struct Hex<'a>(/* private fields */);Expand description
A wrapper type for &[u8] which implements Display by providing a hexdump
See HexDisplayExt for an easier method of constructing this type.
By default, it outputs a lower-case hexdump, but it outputs upper-case if provided with {:X}
formatting option.
use hex_display::Hex;
assert_eq!(
format!("{}", Hex::new(&[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef])),
"0123456789abcdef"
);
assert_eq!(
format!("{:?}", Hex::new(&[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef])),
"Hex(0123456789abcdef)"
);
assert_eq!(
format!("{:X}", Hex::new(&[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef])),
"0123456789ABCDEF"
);The formatter’s width, align, and fill are applied to the entire
output, the same way they would be for any other Display value:
use hex_display::Hex;
assert_eq!(format!("{:>8}", Hex::new(&[0x01, 0x23])), " 0123");Passing the alternate form (#) switches the output to a multiline
hexdump -C-style dump, with an 8-digit offset column, two groups of
eight hex bytes, and an ASCII gutter (non-printable bytes shown as .):
use hex_display::Hex;
let bytes: [u8; 18] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11,
];
assert_eq!(
format!("{:#}", Hex::new(&bytes)),
"\
00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................|
00000010 10 11 |..|",
);The alternate form combines with the upper-case flag ({:#X}) and with
width-padding ({:>WIDTH$#}) just like the single-line form.
If the hex dump length exceeds the width in the output format (longer
than 4 GiB), then the leading digit is replaced with a *:
...
fffffff0 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................|
*0000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................|
*0000010 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................|