File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes UUID to Long or Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "UUID to Long or Integer" Watch "UUID to Long or Integer" New topic

UUID to Long or Integer

Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585

I am generating UUID but in my code i want to convert to Long or Integer, how can I do that?


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15035

I guess you are using class java.util.UUID. The documentation of that class says:

A class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value.

It will not fit in a long (which is 64 bits) and certainly not in an integer (which is 32 bits). It can fit in two longs, and class UUID has two methods: getLeastSignificantBits() and getMostSignificantBits() with which you can get the lower and upper 64 bits of the UUID.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Brian Hammnod

Joined: Sep 10, 2010
Posts: 1
Here is a nice way using NIO, to convert byte[] to long..

java.nio.ByteBuffer.wrap( byte digest[] ).asLongBuffer().get()

You may lose bits, but it works pretty well for around 1million uuids from java.util.UUID.randomUUID().toString() thru org.jasypt.util.digest.Digester using SHA-1

Sometimes you just have to do things even if they are not a great idea.

It was like that when I found it.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45271
Welcome to the Ranch Brian Hammnod
I agree. Here's the link:
subject: UUID to Long or Integer
It's not a secret anymore!