#hex-string #byte-slice #hex #display #format

no-std hex_fmt

将字节切片格式化为十六进制字符串并缩短

3 个版本 (破坏性更新)

0.3.0 2018 年 12 月 27 日
0.2.0 2018 年 10 月 16 日
0.1.0 2018 年 10 月 11 日

#218 in 调试

Download history 28965/week @ 2024-03-14 29556/week @ 2024-03-21 27979/week @ 2024-03-28 30548/week @ 2024-04-04 28651/week @ 2024-04-11 28737/week @ 2024-04-18 24420/week @ 2024-04-25 27753/week @ 2024-05-02 29024/week @ 2024-05-09 27070/week @ 2024-05-16 26657/week @ 2024-05-23 21774/week @ 2024-05-30 21507/week @ 2024-06-06 26403/week @ 2024-06-13 18406/week @ 2024-06-20 14554/week @ 2024-06-27

84,185 次每月下载
323 个 Crates 中使用 (44 直接使用)

MIT/Apache

11KB
168

将字节切片格式化为十六进制字符串并缩短

此 crate 为字节切片和字节切片列表提供包装,实现了标准格式化特性,并将字节打印为十六进制字符串。它尊重对齐、宽度和精度参数,并应用填充和缩短。

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)));

无运行时依赖项