How to calculate the CRC32 of a string in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "hash/crc32"
)

// crc32OfString calculates the CRC32 checksum of a string.
// It returns the checksum as an 8‑character uppercase hex string.
func crc32OfString(s string) string {
    // Use the standard IEEE polynomial (the common CRC‑32 variant)
    table := crc32.MakeTable(crc32.IEEE)

    // Compute the CRC32 checksum of the string's bytes
    crc := crc32.Checksum([]byte(s), table)

    // Format as 8‑digit uppercase hexadecimal
    return fmt.Sprintf("%08X", crc)
}


func main() {
    text := "Go Programming"

    // Compute the CRC32 of the string
    crc := crc32OfString(text)

    fmt.Println("CRC32:", crc)
}



/*
run:

CRC32: 68945C91

*/

 



answered May 8 by avibootz
...