Hex - Leading 0 issue

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)?;

// 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)

// Result is 2f101658f665a6e3677995a5a19f37a3f9670b75970305e898459479961249f

can anyone help me fix this issue? I don't know why the 0 disappear :confused:

I need to do Hex (String) -> U64;4 -> Hex (String)

It needs to be 02f101658f665a6e3677995a5a19f37a3f9670b75970305e898459479961249f, not 2f101658f665a6e3677995a5a19f37a3f9670b75970305e898459479961249f

That's a 64 hex-digit number, if I'm reading it right. Your format string is "{016:x}", which only pads with zero up to 16 digits. Try 064:x instead.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.