以十六进制表示形式显示 u8 切片

问题描述 投票:0回答:5

我需要将

&[u8]
转换为十六进制表示形式。例如
[ A9, 45, FF, 00 ... ]

特征

std::fmt::UpperHex
没有针对切片实现(所以我不能使用
std::fmt::format
)。 Rust 具有
serialize::hex::ToHex
特征,它将
&[u8]
转换为十六进制字符串,但我需要一个具有单独字节的表示。

我可以自己为

UpperHex
实现特质
&[u8]
,但我不确定这有多规范。最规范的方法是什么?

hex rust slice
5个回答
146
投票

Rust 1.26.0 及更高版本

可以使用

:x?
“使用十六进制整数进行调试”格式化程序:

let data = b"hello";
// lower case
println!("{:x?}", data);
// upper case
println!("{:X?}", data);

let data = [0x0, 0x1, 0xe, 0xf, 0xff];
// print the leading zero
println!("{:02X?}", data);
// It can be combined with the pretty modifier as well
println!("{:#04X?}", data);

输出:

[68, 65, 6c, 6c, 6f]
[68, 65, 6C, 6C, 6F]
[00, 01, 0E, 0F, FF]
[
    0x00,
    0x01,
    0x0E,
    0x0F,
    0xFF,
]

如果您需要更多控制或需要支持旧版本的 Rust,请继续阅读。

Rust 1.0 及更高版本

use std::fmt::Write;

fn main() {
    let mut s = String::new();
    for &byte in "Hello".as_bytes() {
        write!(&mut s, "{:X} ", byte).expect("Unable to write");
    }

    println!("{}", s);
}

这可以通过在包装结构上实现格式特征之一(

fmt::Debug
fmt::Display
fmt::LowerHex
fmt::UpperHex
等)并有一个小构造函数来实现:

use std::fmt;

struct HexSlice<'a>(&'a [u8]);

impl<'a> HexSlice<'a> {
    fn new<T>(data: &'a T) -> HexSlice<'a>
    where
        T: ?Sized + AsRef<[u8]> + 'a,
    {
        HexSlice(data.as_ref())
    }
}

// You can choose to implement multiple traits, like Lower and UpperHex
impl fmt::Display for HexSlice<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for byte in self.0 {
            // Decide if you want to pad the value or have spaces inbetween, etc.
            write!(f, "{:X} ", byte)?;
        }
        Ok(())
    }
}

fn main() {
    // To get a `String`
    let s = format!("{}", HexSlice::new("Hello"));

    // Or print it directly
    println!("{}", HexSlice::new("world"));

    // Works with
    HexSlice::new("Hello"); // string slices (&str)
    HexSlice::new(b"Hello"); // byte slices (&[u8])
    HexSlice::new(&"World".to_string()); // References to String
    HexSlice::new(&vec![0x00, 0x01]); // References to Vec<u8>
}

你可以更奇特并创造一个扩展特征:

trait HexDisplayExt {
    fn hex_display(&self) -> HexSlice<'_>;
}

impl<T> HexDisplayExt for T
where
    T: ?Sized + AsRef<[u8]>,
{
    fn hex_display(&self) -> HexSlice<'_> {
        HexSlice::new(self)
    }
}

fn main() {
    println!("{}", "world".hex_display());
}

18
投票

使用

hex
板条箱中的 hex::encode

let a: [u8;4] = [1, 3, 3, 7];
assert_eq!(hex::encode(&a), "01030307");
[dependencies]
hex = "0.4"

10
投票

由于接受的答案不适用于 Rust 1.0 稳定版本,这是我的尝试。应该是无分配的,因此相当快。这基本上是 [u8] 的格式化程序,但由于一致性规则,我们必须将

[u8]
包装为自定义类型
ByteBuf(&[u8])
才能使用它:

struct ByteBuf<'a>(&'a [u8]);

impl<'a> std::fmt::LowerHex for ByteBuf<'a> {
    fn fmt(&self, fmtr: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        for byte in self.0 {
            fmtr.write_fmt(format_args!("{:02x}", byte))?;
        }
        Ok(())
    }
}

用途:

let buff = [0_u8; 24];
println!("{:x}", ByteBuf(&buff));

7
投票

我这样做:

let bytes : Vec<u8> = "привет".to_string().as_bytes().to_vec();
let hex : String = bytes.iter()
  .map(|b| format!("{:02x}", b).to_string())
  .collect::<Vec<String>>()
  .join(" ");

6
投票

有一个板条箱:hex-slice

例如:

extern crate hex_slice;
use hex_slice::AsHex;

fn main() {
    let foo = vec![0u32, 1, 2 ,3];
    println!("{:02x}", foo.as_hex());
}
© www.soinside.com 2019 - 2024. All rights reserved.