spaolacci / murmur3

Native MurmurHash3 Go implementation
BSD 3-Clause "New" or "Revised" License
947 stars 127 forks source link

Consistent result with Guava's implementation #21

Open roberterdin opened 6 years ago

roberterdin commented 6 years ago

I created a function to create a hash string that is consistent with Guava's implementation. Happy to submit a PR for the README since this might be a fairly common use case.

var hexDigits = []byte("0123456789abcdef")
func guavaHash(h1, h2 uint64) string {
    buf := new(bytes.Buffer)
    binary.Write(buf, binary.LittleEndian, h1)
    binary.Write(buf, binary.LittleEndian, h2)
    var result []byte
    for _, curr := range buf.Bytes() {
        result = append(result, hexDigits[(curr>>4)&0xf])
        result = append(result, hexDigits[curr&0xf])
    }
    return string(result)
}

Originally wanted to do a BigInt version as well but since bytes are signed Java and unsigned in Go I passed on that exercise ;)