UUID to BigInteger conversion and BigInteger to UUID conversion

Some times we need to convert UUID to BigInteger and vice versa.
I searched a lot and it takes my time.
Here you can get BigInteger from UUID easily. but when you want to get the same UUID, then it would be a little bit tricky.

First you need to convert the BigInteger no to Hex and then append '-' into uuid.
that's it. but what happen if you get a negative BigInteger number during conversion??
then exception caught during conversion from hex to UUID.
so you need to return a positive bigint number during conversion.
java BigInteger class has a constructor which takes two parameter. the first one defined 1, 0, or -1.
1 defines that it always return a positive that means no leading '-'ve sign in the bigint number and the second one byte[ ] for conversion the actual number.

this method can give us a positive BigInteger
 private static BigInteger getBigIntegerFromUuid(UUID randomUUID) {
     ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
     bb.putLong(randomUUID.getMostSignificantBits());
     bb.putLong(randomUUID.getLeastSignificantBits());
     return new BigInteger(1,bb.array());
   }

here are the complete source code:

1:  import java.math.BigInteger;  
2:  import java.nio.ByteBuffer;  
3:  import java.util.UUID;  
4:  /**  
5:   *  
6:   * @author Ataur  
7:   */  
8:  public class Solution {  
9:    private static BigInteger getBigIntegerFromUuid(UUID randomUUID) {  
10:      ByteBuffer bb = ByteBuffer.wrap(new byte[16]);  
11:      bb.putLong(randomUUID.getMostSignificantBits());  
12:      bb.putLong(randomUUID.getLeastSignificantBits());  
13:      return new BigInteger(1, bb.array());  
14:    }  
15:    public static void runTheMethod(){  
16:      UUID uuid = UUID.randomUUID();  
17:      String leadingZero = null;  
18:      String hexStringWithInsertedHyphens = null;  
19:      BigInteger intValue = getBigIntegerFromUuid(uuid);  
20:      int tmplength =intValue.toString(16).length();  
21:      if(tmplength <32){  
22:        int diff = 32-tmplength;  
23:        String appendValue = "";  
24:        for(int i=0;i<diff;i++){  
25:          appendValue+="0";  
26:        }  
27:        leadingZero = appendValue + intValue.toString(16);  
28:      }  
29:      if (leadingZero != null) {  
30:        hexStringWithInsertedHyphens = leadingZero.replaceFirst("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)", "$1-$2-$3-$4-$5");  
31:      } else {  
32:        hexStringWithInsertedHyphens = intValue.toString(16).replaceFirst("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)", "$1-$2-$3-$4-$5");  
33:      }  
34:      java.util.UUID myUuid = java.util.UUID.fromString(hexStringWithInsertedHyphens);  
35:      System.out.println("myUuid: " + myUuid);  
36:    }  
37:    public static void main(String[] args) {  
38:      int index = 0;  
39:      while(true){  
40:        if (index == 100000)  
41:          break;  
42:        runTheMethod();  
43:        index++;  
44:      }  
45:    }  
46:  }  
please put your comment and ask if you have any query.
Thanks.

Comments

  1. Thank you for sharing. Unfortunally there is bug during the UUID conversion. In the line 23, you test if intValue.toString(16).length() == 31 but it's not enough because it could be lower than 31.

    I fix the conversion like this:
    private static UUID fromBigInteger(BigInteger value) {
    String asString = value.toString(16);
    asString = StringUtils.repeat("0", 32-asString.length()) + asString;
    String hexStringWithInsertedHyphens = asString.replaceFirst("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)", "$1-$2-$3-$4-$5");
    UUID uuid = UUID.fromString(hexStringWithInsertedHyphens);
    return uuid;
    }

    ReplyDelete
    Replies
    1. Hey, Sorry for late replying.In my case say for example 100000 test case, i found some of them go below 30 and 28 etc. so for 28, i append '0000' it works file as well.I updated my tutorial.

      Delete

Post a Comment

Popular posts from this blog

ActiveMQ message producer and consumer with durable subscriber example

Create Maven Local Repository, Upload your own artifact & download jar from local mirror with Artifactory and Nexus OSS