How would I create a java.util.UUID from a string with no dashes?
\"5231b533ba17478798a3f2df37de2aD7\" => #uuid \"5231b533-ba17-4787-98a3-f2df37de2aD7\"
<
I believe the following is the fastest in terms of performance. It is even slightly faster than Long.parseUnsignedLong version . It is slightly altered code that comes from java-uuid-generator.
public static UUID from32(
String id) {
if (id == null) {
throw new NullPointerException();
}
if (id.length() != 32) {
throw new NumberFormatException("UUID has to be 32 char with no hyphens");
}
long lo, hi;
lo = hi = 0;
for (int i = 0, j = 0; i < 32; ++j) {
int curr;
char c = id.charAt(i);
if (c >= '0' && c <= '9') {
curr = (c - '0');
}
else if (c >= 'a' && c <= 'f') {
curr = (c - 'a' + 10);
}
else if (c >= 'A' && c <= 'F') {
curr = (c - 'A' + 10);
}
else {
throw new NumberFormatException(
"Non-hex character at #" + i + ": '" + c + "' (value 0x" + Integer.toHexString(c) + ")");
}
curr = (curr << 4);
c = id.charAt(++i);
if (c >= '0' && c <= '9') {
curr |= (c - '0');
}
else if (c >= 'a' && c <= 'f') {
curr |= (c - 'a' + 10);
}
else if (c >= 'A' && c <= 'F') {
curr |= (c - 'A' + 10);
}
else {
throw new NumberFormatException(
"Non-hex character at #" + i + ": '" + c + "' (value 0x" + Integer.toHexString(c) + ")");
}
if (j < 8) {
hi = (hi << 8) | curr;
}
else {
lo = (lo << 8) | curr;
}
++i;
}
return new UUID(hi, lo);
}