以十六进制表示显示 u8 切片 [英] Show u8 slice in hex representation

查看:56
本文介绍了以十六进制表示显示 u8 切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 &[u8] 转换为十六进制表示.例如 [ A9, 45, FF, 00 ... ].

I need to convert &[u8] to a hex representation. For example [ A9, 45, FF, 00 ... ].

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

The trait std::fmt::UpperHex is not implemented for slices (so I can't use std::fmt::format). Rust has the serialize::hex::ToHex trait, which converts &[u8] to a hex String, but I need a representation with separate bytes.

我可以自己为 &[u8] 实现 trait UpperHex,但我不确定这会有多规范.执行此操作的最规范方法是什么?

I can implement trait UpperHex for &[u8] myself, but I'm not sure how canonical this would be. What is the most canonical way to do this?

推荐答案

Rust 1.26.0 及更高版本

:x?用十六进制整数调试"可以使用格式化程序:

Rust 1.26.0 and up

The :x? "debug with hexadecimal integers" formatter can be used:

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,请继续阅读.

If you need more control or need to support older versions of Rust, keep reading.

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 等)在包装结构上并有一个小构造函数:

This can be fancied up by implementing one of the formatting traits (fmt::Debug, fmt::Display, fmt::LowerHex, fmt::UpperHex, etc.) on a wrapper struct and having a little constructor:

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>
}

你可以更高级,创建一个扩展特性:

You can be even fancier and create an extension trait:

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

这篇关于以十六进制表示显示 u8 切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆