In most cases, you want to parse more than one hex byte at once. In those cases, use the hex crate.
parse this into an integer
You want to use from_str_radix. It's implemented on the integer types.
use std::i64;
fn main() {
let z = i64::from_str_radix("1f", 16);
println!("{:?}", z);
}
If your strings actually have the 0x prefix, then you will need to skip over them. The best way to do that is via trim_start_matches or strip_prefix:
use std::i64;
fn main() {
let raw = "0x1f";
let without_prefix = raw.trim_start_matches("0x");
let z = i64::from_str_radix(without_prefix, 16);
println!("{:?}", z);
}
Answer from Shepmaster on Stack OverflowIn most cases, you want to parse more than one hex byte at once. In those cases, use the hex crate.
parse this into an integer
You want to use from_str_radix. It's implemented on the integer types.
use std::i64;
fn main() {
let z = i64::from_str_radix("1f", 16);
println!("{:?}", z);
}
If your strings actually have the 0x prefix, then you will need to skip over them. The best way to do that is via trim_start_matches or strip_prefix:
use std::i64;
fn main() {
let raw = "0x1f";
let without_prefix = raw.trim_start_matches("0x");
let z = i64::from_str_radix(without_prefix, 16);
println!("{:?}", z);
}
strip_prefix was mentioned earlier, and actually isn't as much trouble to use as it might seem at a casual glance. It can be nested and does the expected thing. Playground Link
fn trimhex(s: &str) -> &str {
s.strip_prefix("0x").unwrap_or(s.strip_prefix("0X").unwrap_or(s))
}
fn main() {
println!("{}", trimhex("0123"));
println!("{}", trimhex("0x01"));
println!("{}", trimhex("0X23"));
// only the first leading 0[xX] is removed
println!("{}", trimhex("0x0x45"));
println!("{}", trimhex("0x0X67"));
println!("{}", trimhex("0X0x89"));
println!("{}", trimhex("0X0Xab"));
}
Hex Leading 0 issue
The zeo disappears, because you reserve space for 16 hex chars only, but u require 64 hex chars. Please also verify if sha256 has can really be stored in an u64 integer!
More on reddit.comHow to easily translate hex string into usize or bytes
Converting &str that contains a decimal or hex representation to a decimal in a generic way
rust - Decimal number to hexadecimal string - Stack Overflow
pub fn hex_to_u64(b: &[u8]) -> Option<u64> {
let a = std::str::from_utf8(b).ok()?;
u64::from_str_radix(a, 16).ok()
}
pub fn parse_sha256_to_u64(str: &str) -> Option<[u64; 4]> {
if str.len() != 64 { return None; }
let mut out = [0u64; 4];
for (chunk, slot) in str.as_bytes().chunks(16).zip(out.iter_mut()) {
*slot = hex_to_u64(chunk)?;
}
Some(out)
}
// Impl in Struct U64;4
pub fn to_hex_string(&self) -> String{
return format!("{:016x}", ByteBuf(self.as_ref()));
}
// That's the string
let contractClientId = "02f101658f665a6e3677995a5a19f37a3f9670b75970305e898459479961249f";
// I convert it to U64;4
let contractClientId_u8 = parse_sha256_to_u64(contractClientId).unwrap();
// Now I convert it back to a String ("Hex" String)
println!("INSURANCE CLIENT ID HASH: {}", contractClientId_u8.to_hex_string());
// Result is 2f101658f665a6e3677995a5a19f37a3f9670b75970305e898459479961249fcan anyone help me fix this issue? I don't know why the 0 disappear :/
I need to do Hex (String) -> U64;4 -> Hex (String)
It needs to be 02f101658f665a6e3677995a5a19f37a3f9670b75970305e898459479961249f, not 2f101658f665a6e3677995a5a19f37a3f9670b75970305e898459479961249f