Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

UUID to Long or Integer

 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks,
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brian Hammnod
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Brian Hammnod
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic