Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
convert a uuid to biginteger
private BigInteger getBigIntegerFromUuid(UUID randomUUID) {
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(randomUUID.getMostSignificantBits());
bb.putLong(randomUUID.getLeastSignificantBits());
return new BigInteger(bb.array());
}
@wluyima

This comment has been minimized.

Copy link

wluyima commented Mar 8, 2017

What if I want to get back an unsigned bitInteger value rather than 2's complement value?

@mariuszs

This comment has been minimized.

Copy link

mariuszs commented Sep 30, 2017

The same but you can reverse this operation (and result value is much smaller):

private static BigInteger pair(UUID uuid) {
	BigInteger value1 = BigInteger.valueOf(uuid.getMostSignificantBits());
	BigInteger value2 = BigInteger.valueOf(uuid.getLeastSignificantBits());
	if (value1.compareTo(value2) < 0) {
		return value2.multiply(value2).add(value1);
	}
	return value1.multiply(value1).add(value1).add(value2);
}
@drmalex07

This comment has been minimized.

Copy link

drmalex07 commented Dec 23, 2017

Another approach using unsigned longs can be found here:
https://gist.github.com/drmalex07/9008c611ffde6cb2ef3a2db8668bc251

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.