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 OverflowConverting &str that contains a decimal or hex representation to a decimal in a generic way
How to easily translate hex string into usize or bytes
Convert hex string to integer in Python - Stack Overflow
beginner - Converting a hexadecimal string to a binary string using Rust pattern matching lookup - Code Review Stack Exchange
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);
}
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"));
}
Without the 0x prefix, you need to specify the base explicitly, otherwise there's no way to tell:
x = int("deadbeef", 16)
With the 0x prefix, Python can distinguish hex and decimal automatically:
>>> print(int("0xdeadbeef", 0))
3735928559
>>> print(int("10", 0))
10
(You must specify 0 as the base in order to invoke this prefix-guessing behavior; if you omit the second parameter, int() will assume base-10.)
int(hexstring, 16) does the trick, and works with and without the 0x prefix:
>>> int("a", 16)
10
>>> int("0xa", 16)
10
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