码迷,mamicode.com
首页 > 其他好文 > 详细

UUID 压缩为22位

时间:2015-09-15 13:03:55      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

public class Generator {

    private static char[] BASE64 = "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789".toCharArray();

    public static String generateUUID() {
        UUID uuid = UUID.randomUUID();
        char[] chs = new char[22];
        long most = uuid.getMostSignificantBits();
        long least = uuid.getLeastSignificantBits();
        int high = (int)((most >> 13) ^ (least >> 31)) & 0x3c;
        int k = chs.length - 1;
        for(int i = 0; i < 10; i++, least >>>= 6) {
            chs[k--] = BASE64[(int)(least & 0x3f)];
        }
        chs[k--] = BASE64[(int)((least & 0x3f) | (most & 0x30))];
        most >>>= 2;
        for(int i = 0; i < 10; i++, most >>>= 6) {
            chs[k--] = BASE64[(int)(most & 0x3f)];
        }
        chs[k--] = BASE64[(int)(high | most)];
        return new String(chs);
    }
}

 

UUID 压缩为22位

标签:

原文地址:http://www.cnblogs.com/dazhaxie/p/4809865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!