jQuery in Action, 2nd edition*
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "UUID to Long or Integer" Watch "UUID to Long or Integer" New topic
Author

UUID to Long or Integer

Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hi,

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

Thanks,


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

Joined: Aug 16, 2005
Posts: 14117
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
Brian Hammnod
Greenhorn

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
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
Welcome to the Ranch Brian Hammnod
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: UUID to Long or Integer