Skip to content

Commit

Permalink
added notes around Guava compatible hash
Browse files Browse the repository at this point in the history
  • Loading branch information
sangupta committed May 12, 2019
1 parent 4d10ab7 commit 56545af
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,48 @@ The library is tested on the following JDK versions:
Implementations were available for `Murmur3` but for some of the legacy code that I maintain, I needed
the `Murmur1` and `Murmur2` hashes. Thus, I ported the original implementations.

You may find the hash inconsistent with [Google Guava]() library. The hash value is the same, it is
the endian-ness of the hash that makes it look different. Refer to [Issue #3](https://github.com/sangupta/murmur/issues/3)
for more details.

To convert the hash into `byte[]` or a `hex-string` you may use the following code:

```java
/**
* Convert a given long value to byte-array.
*
* @param x the long value
*
* @return the byte[] array representation of it
*/
public static byte[] longToBytes(long x) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);

// The ByteOrder.LITTLE_ENDIAN format matches the Google Guava toString() format
buffer.order(ByteOrder.LITTLE_ENDIAN);

buffer.putLong(x);
return buffer.array();
}

/**
* Convert a byte-array to hex string.
*
* @param bytes the byte-array
*
* @return the hex string
*/
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
```

## Features

* Pure Java implementations of various Murmur hashes
Expand Down

0 comments on commit 56545af

Please sign in to comment.