3 个版本 (破坏性更新)
0.3.0 | 2018 年 12 月 27 日 |
---|---|
0.2.0 | 2018 年 10 月 16 日 |
0.1.0 | 2018 年 10 月 11 日 |
#218 in 调试
84,185 次每月下载
在 323 个 Crates 中使用 (44 直接使用)
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)));