cppanda
cppanda

Reputation: 1315

Different values on each hashing approach

can someone please tell me what i'm doing wrong? i'm trying to hash a value with murmurhash, but i get different values every time:

std::string str = "some test string";
char out[32];

MurmurHash3_x86_32(str.c_str(), str.length(), 0, out);
unsigned int hash = reinterpret_cast<unsigned int>(out);

Upvotes: 3

Views: 534

Answers (2)

vdsf
vdsf

Reputation: 1618

It looks to me like MurmurHash3_x86_32 is returning a 32 bits hash value. But you're giving it a 32 bytes output parameter.

You could simply:

std::string str = "some test string";

unsigned int hash;
MurmurHash3_x86_32(str.c_str(), str.length(), 0, &hash);

Upvotes: 4

Tim Martin
Tim Martin

Reputation: 3697

The variable out is of type char [], or array of char. This acts as a pointer to char in most contexts. Here, you're casting the pointer value, not the pointed-to contents.

Also, I don't know the MurmurHash API, but the array out is 32 bytes. You're attempting to cast it to a 32 bit integer (assuming integers are 32-bit on your platform).

Upvotes: 9

Related Questions