随机生成汉字与md5码生成

/*
* 摘抄:
* 汉字的机内码从第16区B0开始,并且从区位D7开始以后的汉字都是很难见到的繁杂汉字, 可以将这些排除。
* 所以随机生成汉字机字码的第1位范围在B、C、D之间。
* 如果第1位是D,则第2位区位码就不能是7以后的16进制数,
* 由于每个区的第1个位置和最后一个位置是空的,没有汉字,因此:
* 生成的区位码的第3位如果是A,第4位就不能是0,如果是F,第4位就不能是F。
*/
public static void main(String[] args) {
Random rand = new Random();
String[] hex = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
int r1 = rand.nextInt(3) + 11;
String strR1 = hex[r1];
int r2;
if(r1 == 13){
r2 = rand.nextInt(7);
}else {
r2 = rand.nextInt(16);
}
String strR2 = hex[r2];

int r3 = rand.nextInt(6)+10;
String strR3 = hex[r3];
int r4 ;
if(r3 ==  10){
r4 = rand.nextInt(15) + 1;
}else if(r3 == 15){
r4 = rand.nextInt(15);
}else{
r4 = rand.nextInt(16);
}
String strR4 = hex[r4];

int low = Integer.parseInt(strR1 + strR2, 16);
int high = Integer.parseInt(strR3 + strR4, 16);
byte[] num = new byte[2];
num[0] = (byte)low;
num[1] = (byte)high;

System.out.println(low + ", " + high);
System.out.println(num[0]  + ", " + num[1]);
System.out.println(new String(num));

/* md5码生成*/
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("MD5");
} catch (Exception e) {
e.printStackTrace();
}
String feifei = "feifei-菲菲";
digest.update(feifei.getBytes());
byte[] bytes = digest.digest();
StringBuilder md5Str = new StringBuilder();

for(int i = 0; i < bytes.length; i++){
int hexNum = bytes[i] & 0xFF;
if(hexNum < 16){
md5Str.append(0);
}
md5Str.append(Integer.toHexString(hexNum));
}

System.out.println(md5Str.toString());

}



顺带加上编码解决问题;



int start = 19968;
int end = 40869;
int distance = end - start;
long random = Math.round(Math.random() * (distance + 1) + 19968);
char c = (char) (Integer.valueOf(Long.toHexString(random), 16)
.intValue());


System.out.println(c);

猜你喜欢

转载自blog.csdn.net/w358637319/article/details/40736359