简体   繁体   English

如何在 Rust 中将字符串转换为十六进制?

[英]How do I convert a string to hex in Rust?

I want to convert a string of characters (a SHA256 hash) to hex in Rust:我想在 Rust 中将字符串(SHA256 哈希)转换为十六进制:

extern crate crypto;
extern crate rustc_serialize;

use rustc_serialize::hex::ToHex;
use crypto::digest::Digest;
use crypto::sha2::Sha256;

fn gen_sha256(hashme: &str) -> String {
    let mut sh = Sha256::new();
    sh.input_str(hashme);

    sh.result_str()
}

fn main() {
    let hash = gen_sha256("example");

    hash.to_hex()
}

The compiler says:编译器说:

error[E0599]: no method named `to_hex` found for type `std::string::String` in the current scope
  --> src/main.rs:18:10
   |
18 |     hash.to_hex()
   |          ^^^^^^

I can see this is true;我可以看到这是真的; it looks like it's only implemented for [u8] .看起来它只是为[u8]实现的

What am I to do?我是什么做的? Is there no method implemented to convert from a string to hex in Rust?在 Rust 中是否没有实现从字符串转换为十六进制的方法?

My Cargo.toml dependencies:我的 Cargo.toml 依赖项:

[dependencies]
rust-crypto = "0.2.36"
rustc-serialize = "0.3.24"

edit I just realized the string is already in hex format from the rust-crypto library.编辑我刚刚意识到该字符串已经是 rust-crypto 库中的十六进制格式。 D'oh.哦。

I will go out on a limb here, and suggest that the solution is for hash to be of type Vec<u8> .我将在这里尝试一下,并建议解决方案是将hash设为Vec<u8>类型。


The issue is that while you can indeed convert a String to a &[u8] using as_bytes and then use to_hex , you first need to have a valid String object to start with.问题是,虽然您确实可以使用as_bytesString转换为&[u8] ,然后使用to_hex ,但首先需要有一个有效的String对象。

While any String object can be converted to a &[u8] , the reverse is not true.虽然任何String对象都可以转换为&[u8] ,但反之则不然。 A String object is solely meant to hold a valid UTF-8 encoded Unicode string: not all bytes pattern qualify. String对象仅用于保存有效的 UTF-8 编码的 Unicode 字符串:并非所有字节模式都符合条件。

Therefore, it is incorrect for gen_sha256 to produce a String .因此, gen_sha256生成String是不正确的。 A more correct type would be Vec<u8> which can, indeed, accept any bytes pattern.更正确的类型是Vec<u8> ,它确实可以接受任何字节模式。 And from then on, invoking to_hex is easy enough:从那时起,调用to_hex容易了:

hash.as_slice().to_hex()

It appears the source for ToHex has the solution I'm looking for.看来ToHex的来源有我正在寻找的解决方案。 It contains a test:它包含一个测试:

#[test]
pub fn test_to_hex() {
    assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172");
}

My revised code is:我修改后的代码是:

let hash = gen_sha256("example");

hash.as_bytes().to_hex()

This appears to work.这似乎有效。 I will take some time before I accept this solution if anyone has an alternative answer.如果有人有其他答案,我会花一些时间才能接受此解决方案。

A hexadecimal representation can be generated with a function like this:可以使用如下函数生成十六进制表示:

pub fn hex_push(buf: &mut String, blob: &[u8]) {
    for ch in blob {
        fn hex_from_digit(num: u8) -> char {
            if num < 10 {
                (b'0' + num) as char
            } else {
                (b'A' + num - 10) as char
            }
        }
        buf.push(hex_from_digit(ch / 16));
        buf.push(hex_from_digit(ch % 16));
    }
}

This is a tad more efficient than the generic radix formatting implemented currently in the language .这比当前在该语言中实现通用基数格式要高效一点。

Here's a benchmark :这是一个基准

test bench_specialized_hex_push   ... bench:          12 ns/iter (+/- 0) = 250 MB/s
test bench_specialized_fomat      ... bench:          42 ns/iter (+/- 12) = 71 MB/s
test bench_specialized_format     ... bench:          47 ns/iter (+/- 2) = 63 MB/s
test bench_specialized_hex_string ... bench:          76 ns/iter (+/- 9) = 39 MB/s
test bench_to_hex                 ... bench:          82 ns/iter (+/- 12) = 36 MB/s
test bench_format                 ... bench:          97 ns/iter (+/- 8) = 30 MB/s

Thanks to the user j ey in the ##rust irc channel in freenode.多亏了用户j ey在freenode的所述##锈IRC频道。 You can just use the hex representation fmt provides,您可以使用fmt提供的十六进制表示,

>> let mut s = String::new();
>> use std::fmt::Write as FmtWrite; // renaming import to avoid collision
>> for b in "hello world".as_bytes() { write!(s, "{:02x}", b); }
()
>> s
"68656c6c6f20776f726c64"
>> 

or a bit silly one,或者有点傻,

>> "hello world".as_bytes().iter().map(|x| format!("{:02x}", x)).collect::<String>()
"68656c6c6f20776f726c64"

Using the hex crate, it is very easy:使用hex crate 非常简单:

use hex;
println!("{}", hex::encode(String("some str")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM