I'm looking to get a hash value for string and integer inputs. Using murmurhash3, I'm able to do it for strings but not integers:
pip install murmurhash3
import mmh3
mmh3.hash(34)
Returns the following error:
TypeError: a bytes-like object is required, not 'int'
I could convert it to bytes like this:
mmh3.hash(bytes(34))
But then I'll get an error message if the input is string
How do I overcome this without converting the integer to string?
You can't. Or more precisely, you need to convert it to
bytes
orstr
in some way, but it needn't be a human-readable text form likeb'34'
/'34'
. A common approach on Python 3 would be:which makes a minimalist raw bytes representation of the original
int
(regardless of length); for34
, you'd getb'"'
(because it only takes one byte to store it, so you're basically getting abytes
object with its ordinal value), but for largerint
s it still works (unlike mucking about withchr
), and it's always as small as possible (getting 8 bits of data per byte, rather than a titch over 3 bits per byte as you'd get converting to a text string).If you're on Python 2 (WHY?!? It's been end-of-life for nearly a year),
int.to_bytes
doesn't exist, but you can fake it with moderate efficiency in various ways, e.g. (only handling non-negative values, unliketo_bytes
which handles signed values with a simple flag):