首页 技术 正文
技术 2022年11月6日
0 收藏 411 点赞 1,051 浏览 1791 个字
package test1;import java.util.Random;
import java.util.UUID;
public class ShareCodeUtil {
private static final char[] r=new char[]{'q', 'w', 'e', '8', 'a', 's', '2', 'd', 'z',
'x', '9', 'c', '7', 'p', '5', 'i', 'k', '3', 'm', 'j', 'u', 'f', 'r', '4', 'v',
'y', 'l', 't', 'n', '6', 'b', 'g', 'h'};
private static final char b='o';
private static final int binLen=r.length;
private static final int s=6;
public static String toSerialCode(long id) {
char[] buf=new char[32];
int charPos=32;
while((id / binLen) > 0) {
int ind=(int)(id % binLen);
// System.out.println(num + "-->" + ind);
buf[--charPos]=r[ind];
id /= binLen;
}
buf[--charPos]=r[(int)(id % binLen)];
// System.out.println(num + "-->" + num % binLen);
String str=new String(buf, charPos, (32 - charPos));
// 不够长度的自动随机补全
if(str.length() < s) {
StringBuilder sb=new StringBuilder();
sb.append(b);
Random rnd=new Random();
for(int i=1; i < s - str.length(); i++) {
sb.append(r[rnd.nextInt(binLen)]);
}
str+=sb.toString();
}
return str;
} public static long codeToId(String code) {
char chs[]=code.toCharArray();
long res=0L;
for(int i=0; i < chs.length; i++) {
int ind=0;
for(int j=0; j < binLen; j++) {
if(chs[i] == r[j]) {
ind=j;
break;
}
}
if(chs[i] == b) {
break;
}
if(i > 0) {
res=res * binLen + ind;
} else {
res=ind;
}
// System.out.println(ind + "-->" + res);
}
return res;
}
public static String[] chars = new String[] { "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", "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" };
public static String generateShortUuid() {
StringBuffer shortBuffer = new StringBuffer();
String uuid = UUID.randomUUID().toString().replace("-", "");
for (int i = 0; i < 8; i++) {
String str = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x % 0x3E]);
}
return shortBuffer.toString(); } public static void main(String[] args) { System.out.println(generateShortUuid());
}
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902