I'm using this code to compact my UUIDs down to 22 chars strings :
public static String asBase64String(UUID uuid) {
return Base64.encodeBase64String(asByteArray(uuid));
}
public static byte[] asByteArray(UUID uuid) {
long msb = uuid.getMostSignificantBits();
long lsb = uuid.getLeastSignificantBits();
byte[] buffer = new byte[16];
for (int i = 0; i < 8; i++) {
buffer[i] = (byte) (msb >>> 8 * (7 - i));
}
for (int i = 8; i < 16; i++) {
buffer[i] = (byte) (lsb >>> 8 * (7 - i));
}
return buffer;
}
I'm using Apache Commons Codec for Base64 computations but other
librairies are available.