首页 > 其他 > 详细

UUID 压缩为22位

时间:2015-09-15 13:01:43      阅读:279      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!