Creating a UUID from a string with no dashes

前端 未结 10 2260
臣服心动
臣服心动 2020-11-29 01:15

How would I create a java.util.UUID from a string with no dashes?

\"5231b533ba17478798a3f2df37de2aD7\" => #uuid \"5231b533-ba17-4787-98a3-f2df37de2aD7\"
<         


        
10条回答
  •  我在风中等你
    2020-11-29 01:43

    Another solution would be something similar to Pawel's solution but without creating new Strings and only solving the questions problem. If perfomance is a concern, avoid regex/split/replaceAll and UUID.fromString like the plague.

    String hyphenlessUuid = in.nextString();
    BigInteger bigInteger = new BigInteger(hyphenlessUuid, 16);
     new UUID(bigInteger.shiftRight(64).longValue(), bigInteger.longValue());
    

提交回复
热议问题