今天看到一个UUID的用法,19位62进制UUID,感觉可以节省不少空间,非常好用,推荐一下。
根据原博客改编,地址https://blog.csdn.net/weixin_34034261/article/details/91585357。
/**
* 以62进制(字母加数字)生成19位UUID,最短的UUID
* @author Jintao
* copy from csdn <a href="https://blog.csdn.net/weixin_34034261/article/details/91585357">超短的19位UUID</>
*/
public class UUID19 {
private final static char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z' };
/**
* 支持的最大进制数
*/
private static final int MAX_RADIX = DIGITS.length;
/**
* 支持的最小进制数
*/
private static final int MIN_RADIX = 2;
/**
* 将长整型数值转换为指定的进制数(最大支持62进制,字母数字已经用尽)
*
*/
public static String toString(long i, int radix) {
if (radix < MIN_RADIX || radix > MAX_RADIX) {
radix = 10;
}
if (radix == 10) {
return Long.toString(i);
}
final int size = 65;
int charPos = 64;
char[] buf = new char[size];
boolean negative = (i < 0);
if (!negative) {
i = -i;
}
while (i <= -radix) {
buf[charPos--] = DIGITS[(int) (-(i % radix))];
i = i / radix;
}
buf[charPos] = DIGITS[(int) (-i)];
if (negative) {
buf[--charPos] = '-';
}
return new String(buf, charPos, (size - charPos));
}
private static String digits(long val, int digits) {
long hi = 1L << (digits * 4);
return toString(hi | (val & (hi - 1)), MAX_RADIX)
.substring(1);
}
/**
* 以62进制(字母加数字)生成19位UUID,最短的UUID
*/
public static String uuid() {
UUID uuid = UUID.randomUUID();
return digits(uuid.getMostSignificantBits() >> 32, 8) +
digits(uuid.getMostSignificantBits() >> 16, 4) +
digits(uuid.getMostSignificantBits(), 4) +
digits(uuid.getLeastSignificantBits() >> 48, 4) +
digits(uuid.getLeastSignificantBits(), 12);
}
}