-2

I tried https://github.com/spaolacci/murmur3,roberson-io/mmh3 and many go version. But I get the different result from python mmh3.

Python example:

import mmh3
print(mmh3.hash("foods",45))

Go example:

package main

import (
    "fmt"

    "github.com/spaolacci/murmur3"
)

func main() {

    mHash := murmur3.New32WithSeed(45)
    mHash.Write([]byte("foods"))
    hashNum := mHash.Sum32()
    fmt.Println(hashNum)

    fmt.Printf("%d\n", murmur3.Sum64WithSeed([]byte("foods"), 45))
}

I want to get same hash value with python mmh3. So how to get the same hash value with mmh3 from python?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57

1 Answers1

0

All those hash methods return unsigned integers in Go:

func (Hash32) Sum32() uint32

func Sum64WithSeed(data []byte, seed uint32) uint64

While in Python they return signed integers by default:

hash(key[, seed=0, signed=True]) -> hash value   Return a 32 bit integer.

If you want the same decimal number, you either have to return an unsigned int from Python:

# 3049460612
print(mmh3.hash("foods",45,signed=False))

Or a signed int from Go:

mHash := murmur3.New32WithSeed(45)
mHash.Write([]byte("foods"))
hashNum := mHash.Sum32()
// -1245506684
fmt.Println(int32(hashNum))
Fiduro
  • 88
  • 1
  • 6