banner
davirain

davirain

twitter
github
知乎
twitter

Conversion between Rust decimal and hexadecimal

In Rust, you can use the format!() macro to format decimal numbers into hexadecimal strings. Here is a simple example implementation:

fn decimal_to_hex(decimal: u32) -> String {
    format!("{:X}", decimal)
}

Here, {} is used as the formatting string, which converts an unsigned decimal number into an uppercase hexadecimal string. For example, to convert the number 123456789 into a hexadecimal string, you can call the function like this:

let hex_string = decimal_to_hex(123456789);
println!("{}", hex_string); // Output: "75BCD15"

Note that if you need to handle larger numbers, you can use the u64 or u128 types. If you need to convert a hexadecimal string back into a decimal number, you can use the u64::from_str_radix() function. For example:

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.