#format #hex #display #debug

no-std hex_fmt

Formatting and shortening byte slices as hexadecimal strings

3 releases (breaking)

0.3.0 Dec 27, 2018
0.2.0 Oct 16, 2018
0.1.0 Oct 11, 2018

#163 in Debugging

Download history 12802/week @ 2021-06-07 13110/week @ 2021-06-14 10566/week @ 2021-06-21 10774/week @ 2021-06-28 13087/week @ 2021-07-05 11467/week @ 2021-07-12 11244/week @ 2021-07-19 12223/week @ 2021-07-26 15403/week @ 2021-08-02 12051/week @ 2021-08-09 11268/week @ 2021-08-16 13338/week @ 2021-08-23 14730/week @ 2021-08-30 14057/week @ 2021-09-06 17286/week @ 2021-09-13 15339/week @ 2021-09-20

41,077 downloads per month
Used in 89 crates (26 directly)

MIT/Apache

11KB
168 lines

Formatting and shortening byte slices as hexadecimal strings

This crate provides wrappers for byte slices and lists of byte slices that implement the standard formatting traits and print the bytes as a hexadecimal string. It respects the alignment, width and precision parameters and applies padding and shortening.

let bytes: &[u8] = &[0x0a, 0x1b, 0x2c, 0x3d, 0x4e, 0x5f];

assert_eq!("0a1b2c3d4e5f", &format!("{}", HexFmt(bytes)));

// By default the full slice is printed. Change the width to apply padding or shortening.
assert_eq!("0a..5f", &format!("{:6}", HexFmt(bytes)));
assert_eq!("0a1b2c3d4e5f", &format!("{:12}", HexFmt(bytes)));
assert_eq!("  0a1b2c3d4e5f  ", &format!("{:16}", HexFmt(bytes)));

// The default alignment is centered. Use `<` or `>` to align left or right.
assert_eq!("0a1b..", &format!("{:<6}", HexFmt(bytes)));
assert_eq!("0a1b2c3d4e5f    ", &format!("{:<16}", HexFmt(bytes)));
assert_eq!("..4e5f", &format!("{:>6}", HexFmt(bytes)));
assert_eq!("    0a1b2c3d4e5f", &format!("{:>16}", HexFmt(bytes)));

// Use e.g. `4.8` to set the minimum width to 4 and the maximum to 8.
assert_eq!(" 12 ", &format!("{:4.8}", HexFmt([0x12])));
assert_eq!("123456", &format!("{:4.8}", HexFmt([0x12, 0x34, 0x56])));
assert_eq!("123..89a", &format!("{:4.8}", HexFmt([0x12, 0x34, 0x56, 0x78, 0x9a])));

// If you prefer uppercase, use `X`.
assert_eq!("0A1B..4E5F", &format!("{:X}", HexFmt(bytes)));

// All of the above can be combined.
assert_eq!("0A1B2C..", &format!("{:<4.8X}", HexFmt(bytes)));

// With `HexList`, the parameters are applied to each entry.
let list = &[[0x0a; 3], [0x1b; 3], [0x2c; 3]];
assert_eq!("[0A.., 1B.., 2C..]", &format!("{:<4X}", HexList(list)));

No runtime deps