How do I convert a string to hex in Rust?

leshow

I want to convert a string of characters (a SHA256 hash) to hex in Rust:

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].

What am I to do? Is there no method implemented to convert from a string to hex in Rust?

My Cargo.toml dependencies:

[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. D'oh.

Matthieu M.

I will go out on a limb here, and suggest that the solution is for hash to be of type 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.

While any String object can be converted to a &[u8], the reverse is not true. A String object is solely meant to hold a valid UTF-8 encoded Unicode string: not all bytes pattern qualify.

Therefore, it is incorrect for gen_sha256 to produce a String. A more correct type would be Vec<u8> which can, indeed, accept any bytes pattern. And from then on, invoking to_hex is easy enough:

hash.as_slice().to_hex()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert a string into HEX representation?

How do I convert a String to an integer in Rust?

How do I convert a hex string to an Ascii string in Kusto

In Java, how do I convert a hex string to a byte[]?

How do I convert my HEX string into a number?

How do i convert string.byte to their ascii hex value

How do I convert a hex value that is stored as a string into a hex value in python?

How do I convert a string into a vector of bytes in rust?

How to convert hex string to a f64 in Rust?

How do I convert hex to decimal in Python?

How do I convert hex to decimal in Python?

in C# how do I convert what I think is a string of hex ascii to something i can read?

How do I convert sum of hex values back to hex values

How do I actually decode a string to hex?

How can I convert a String into a Vector in Rust?

How do I convert a ASCII hex string (ex. "6C" to "l") in javascipt?

In Java, how do I convert a byte array to a string of hex digits while keeping leading zeros?

How do I convert a floating-point number into a hex string in Perl?

How do I convert a boolean to an integer in Rust?

How do I convert a C string into a Rust string and back via FFI?

How do you convert a `std::string` hex value to an `unsigned char`

How to convert hex string to hex number in nodejs

How to convert a hex string to hex number

How to convert Hex in string format to Hex format

How do I split a string in Rust?

How do I treat a String as a File in Rust?

how can I convert my String (that represents hex values) to bytes?

How can I convert a hex string to a u8 slice?

How can I convert a hex string to a byte array?