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 Overflow
🌐
W3Resource
w3resource.com › rust › error_handling › rust-result-and-option-types-exercise-7.php
Rust Function: Hex string to integer
June 10, 2025 - fn hex_string_to_int(hex_string: &str) -> Option<u64> { // Attempt to parse the hexadecimal string into a u64 integer match u64::from_str_radix(hex_string, 16) { // If parsing succeeds, return the parsed integer wrapped in Some Ok(parsed_int) ...
Discussions

How to easily translate hex string into usize or bytes
for example, I'm gotting "0x2A2F" and how to translate it into the actual number? I want a high performance way without other crates. Or if there is a good crate implemented for it, I'm going to learn about it. More on users.rust-lang.org
🌐 users.rust-lang.org
2
0
January 3, 2024
Converting &str that contains a decimal or hex representation to a decimal in a generic way
I was trying to write a generic function that could take a &str containing either a decimal representation 123, or a hexadecimal representation 0xFF and parse this into a decimal number. use num_traits::Num; fn parse … More on users.rust-lang.org
🌐 users.rust-lang.org
6
0
December 14, 2020
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.com
🌐 r/rust
7
4
May 14, 2019
rust - How can I convert a hex string to a u8 slice? - Stack Overflow
I have a string that looks like this "090A0B0C" and I would like to convert it to a slice that looks something like this [9, 10, 11, 12]. How would I best go about doing that? I don't want to conv... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › rust-lang › rfcs › issues › 1098
Parsing string to integer accepting '0x' + hexadecimal · Issue #1098 · rust-lang/rfcs
April 29, 2015 - In many cases, it is useful for a program that parses integers (e.g. from the command line) to accept both decimal and hexadecimal constants, by letting the user prefix the latter with 0x. Currently, FromStr::from_str (also the destination of str::parse) only accepts base 10; from_str_radix allows an arbitrary base to be specified, but doesn't have any special magic to parse prefixes. If I were writing from_str from scratch, I would suggest having it accept 0x, perhaps along with 0o and 0b like Rust ...
Author: rust-lang
🌐
mkaz.blog
mkaz.blog › working-with-rust › numbers
Numbers - mkaz.blog
Or when converting string to float, specify the float type inline to the parse function, using ::<> syntax: let str = "2.71828"; let e = str.parse::<f32>().unwrap(); To convert a single char to an integer in Rust, use .to_digit(RADIX). The radix value is used for conversion, 10 for decimal, ...
🌐
Medium
medium.com › @dlcoder › using-hexadecimal-in-rust-a-comprehensive-guide-71160d483286
Using Hexadecimal in Rust: A Comprehensive Guide | by David Li | Medium
May 21, 2023 - In Rust, you can represent hexadecimal literals using the 0x prefix. For example, the following code snippet declares a hexadecimal integer:
🌐
Rust Programming Language
users.rust-lang.org › help
How to easily translate hex string into usize or bytes - help - The Rust Programming Language Forum
January 3, 2024 - for example, I'm gotting "0x2A2F" and how to translate it into the actual number? I want a high performance way without other crates. Or if there is a good crate implemented for it, I'm going to learn about it.
🌐
Rust
doc.rust-lang.org › std › fmt › trait.UpperHex.html
UpperHex in std::fmt - Rust
The UpperHex trait should format its output as a number in hexadecimal, with A through F in upper case. For primitive signed integers (i8 to i128, and isize), negative values are formatted as the two’s complement representation.
🌐
Programming Idioms
programming-idioms.org › idiom › 142 › hexadecimal-digits-of-an-integer › 2346 › rust
Hexadecimal digits of an integer, in Rust
template <typename I> std::string n2hexstr(I w, size_t hex_len = sizeof(I)<<1) { static const char* digits = "0123456789ABCDEF"; std::string str(hex_len, '-'); for (size_t i=0, j=(hex_len-1)*4 ; i<hex_len; ++i,j-=4) str[i] = digits[(w>>j) & 0x0f]; return str; } ... S = io_lib:fwrite("~.16B",[X]). ...
Find elsewhere
🌐
FriendlyUsers Tech Blog
friendlyuser.github.io › posts › tech › rust › Using_Hexadecimal_in_Rust_A_Comprehensive_Guide
Using Hexadecimal in Rust A Comprehensive Guide - FriendlyUsers Tech Blog
To convert a hexadecimal string to a decimal integer, you can use the u32::from_str_radix method (or any other appropriate integer type depending on the range of your hexadecimal value):
🌐
Rust
docs.rs › hex
hex - Rust
Encoding and decoding hex strings. For most cases, you can simply use the decode, encode and encode_upper functions. If you need a bit more control, use the traits ToHex and FromHex instead.
🌐
Rust Programming Language
users.rust-lang.org › help
Converting &str that contains a decimal or hex representation to a decimal in a generic way - help - The Rust Programming Language Forum
December 14, 2020 - I was trying to write a generic function that could take a &str containing either a decimal representation 123, or a hexadecimal representation 0xFF and parse this into a decimal number. use num_traits::Num; fn parse …
🌐
Rust FAQ
rustfaq.org › home › strings › how to convert a string to an integer in rust
How to Convert a String to an Integer in Rust — Rust FAQ
Use i64::from_str_radix(str, 16) when you are working with hexadecimal memory addresses or color codes and need explicit base control. Use usize for array indices and isize for pointer arithmetic, but stick to i32 or i64 for domain values to avoid platform-dependent size surprises.
🌐
crates.io
crates.io › crates › parse_int
parse_int - crates.io: Rust Package Registry
April 11, 2025 - Parse &str with common prefixes to integer values · #binary · #decimal · #hex · #hexadecimal ·
🌐
Reddit
reddit.com › r/rust › hex leading 0 issue
r/rust on Reddit: Hex Leading 0 issue
May 14, 2019 -
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 2f101658f665a6e3677995a5a19f37a3f9670b75970305e898459479961249f

can 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

🌐
Rust Documentation
doc.rust-lang.org › stable › rust-by-example › primitives › literals.html
Literals and operators - Rust By Example
Integers 1, floats 1.2, characters 'a', strings "abc", booleans true and the unit type () can be expressed using literals. Integers can, alternatively, be expressed using hexadecimal, octal or binary notation using these prefixes respectively: 0x, 0o or 0b. Underscores can be inserted in numeric ...
🌐
DEV Community
dev.to › sharmaprash › how-to-convert-a-string-to-an-integer-in-rust-1f0n
how to convert a String to an Integer in Rust - DEV Community
March 12, 2024 - Converting a string to an integer in Rust is straightforward using the parse() method. Remember to handle potential parsing errors appropriately to ensure the robustness of your code.
🌐
Rust Programming Language
users.rust-lang.org › t › how-would-i-store-hexedecimal-values-in-a-variable › 45545
How would I store hexedecimal values in a variable? - The Rust Programming Language Forum
July 8, 2020 - extern crate hex; fn main() { let blue: u32 = hex::decode("0c1047"); // Error over here } So I want blue to store #0c1047 this hexadecimal value inside blue (here is the color picker site https://duckduckgo.com/?q=…
🌐
crates.io
crates.io › crates › hexstring
hexstring - crates.io: Rust Package Registry
February 11, 2026 - It allows all the common conversion expected from a hexadecimal string :