How to calculate the CRC32 of a string in Rust

1 Answer

0 votes
use crc32fast::Hasher;

// Calculates the CRC32 checksum of a string.
// Returns an 8‑character uppercase hexadecimal string.
fn crc32_of_string(input: &str) -> String {
    // Create a CRC32 hasher
    let mut hasher = Hasher::new();

    // Feed the UTF‑8 bytes of the string into the hasher
    hasher.update(input.as_bytes());

    // Finalize and get the CRC32 value (u32)
    let crc = hasher.finalize();

    // Format as 8‑digit uppercase hex (standard CRC32 format)
    format!("{:08X}", crc)
}

fn main() {
    let text = "Rust Programming";

    // Compute the CRC32 of the string
    let crc = crc32_of_string(text);

    println!("CRC32: {}", crc);
}



/*
run:

CRC32: CE0235F5

*/

 



answered May 8 by avibootz
...