Creating a UUID from a string with no dashes

前端 未结 10 2181
臣服心动
臣服心动 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:46

    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);
    }
    
    0 讨论(0)
  • 2020-11-29 01:48
    public static String addUUIDDashes(String idNoDashes) {
        StringBuffer idBuff = new StringBuffer(idNoDashes);
        idBuff.insert(20, '-');
        idBuff.insert(16, '-');
        idBuff.insert(12, '-');
        idBuff.insert(8, '-');
        return idBuff.toString();
    }
    

    Maybe someone else can comment on the computational efficiency of this approach. (It wasn't a concern for my application.)

    0 讨论(0)
  • 2020-11-29 01:50

    Clojure's #uuid tagged literal is a pass-through to java.util.UUID/fromString. And, fromString splits it by the "-" and converts it into two Long values. (The format for UUID is standardized to 8-4-4-4-12 hex digits, but the "-" are really only there for validation and visual identification.)

    The straight forward solution is to reinsert the "-" and use java.util.UUID/fromString.

    (defn uuid-from-string [data]
      (java.util.UUID/fromString
       (clojure.string/replace data
                               #"(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})"
                               "$1-$2-$3-$4-$5")))
    

    If you want something without regular expressions, you can use a ByteBuffer and DatatypeConverter.

    (defn uuid-from-string [data]
      (let [buffer (java.nio.ByteBuffer/wrap 
                     (javax.xml.bind.DatatypeConverter/parseHexBinary data))]
        (java.util.UUID. (.getLong buffer) (.getLong buffer))))
    
    0 讨论(0)
  • 2020-11-29 01:55

    You could do a goofy regular expression replacement:

    String digits = "5231b533ba17478798a3f2df37de2aD7";                         
    String uuid = digits.replaceAll(                                            
        "(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})",                            
        "$1-$2-$3-$4-$5");                                                      
    System.out.println(uuid); // => 5231b533-ba17-4787-98a3-f2df37de2aD7
    
    0 讨论(0)
提交回复
热议问题