9

I'm trying to make the java UUID shorter while preserving the same uniqueness as the UUID has. I wrote the following code:

public static void main(String[] args) {
    UUID uid=UUID.randomUUID();
    String shortId=to62System(uid.getMostSignificantBits())+
        to62System(uid.getLeastSignificantBits());

    System.out.println(shortId);
}

static char[] DIGITS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
static int RADIX = DIGITS.length;

public static String to62System(long value) {
    if (value == 0) {
        return "0";
    } else {
        char[] buf = new char[11];
        int charPos = 10;
        long i = value;
        while (i != 0) {
            buf[charPos--] = DIGITS[Math.abs((int) (i % RADIX))];
            i /= RADIX;
        }
        return new String(buf, charPos + 1, (10 - charPos));
    }
}

Am I doing it right or did I overlooked something important?

MariuszS
  • 30,646
  • 12
  • 114
  • 155
Fipil
  • 291
  • 2
  • 5
  • 16
  • 2
    Just curious, why would you do this? – Zavior Dec 03 '13 at 20:31
  • 2
    You can't: http://blogs.msdn.com/b/oldnewthing/archive/2008/06/27/8659071.aspx (edit: I'm not actually sure if randomUUID() uses the same algorithm, but chances are good that it's still not a great idea). – Sean Dec 03 '13 at 20:33
  • 1
    Unique over what space? That constrains the size of the ID. – WPrecht Dec 03 '13 at 20:33
  • @Zavior I need as short as possible **worldwide** unique id, usable in URL. It has to be short for example to be usable in the tweets. Yes, there are some url-shortening services, but I donnot want to use it. – Fipil Dec 05 '13 at 19:28
  • @Sean, I'm not sure that you studied my code example enough. As described in the article you pointed, someone gets just part of GUID and thinks that it's still unique. But it's not what I'm doing in my code example. The UUID consists of two longs, it's 16 bytes. I donnot cut any part of the bytes, I'm just changing each long from one number system (decimal) to another (radix=62). So the same number is then represented by shorter string.Then I concatenate these two strings. – Fipil Dec 05 '13 at 19:36
  • 1
    @WPrecht worldwide unique. – Fipil Dec 05 '13 at 19:37

3 Answers3

5

I use org.apache.commons.codec.binary.Base64 to convert a UUID into a url-safe unique string that is 22 characters in length and has the same uniqueness as UUID.

I posted my code on Storing UUID as base64 String

Community
  • 1
  • 1
stikkos
  • 1,916
  • 2
  • 19
  • 34
3

Take a look at FriendlyId library. This library allow to encode UUID to Base62 string (Url62) and back. Uniqueness is achieved and encoded string is shorter.

https://github.com/Devskiller/friendly-id

MariuszS
  • 30,646
  • 12
  • 114
  • 155
-1

I believe even once you get it down to 22 characters by changing to base whatever, you can safely truncate a few characters and still be reasonably certain you won't get any collisions, as you probably know the astronomically large numbers involved. LOL loved the first guy's response thinking u were just like grabbing some characters from a standard UUID and calling it a day haha

Luke Visinoni
  • 91
  • 1
  • 9