近来新学到一方法,随机生成汉字的,小结如下:
public static String getChineseCharacter(long seed) throws Exception {
String str = null;
int highPos, lowPos;
Random random = new Random(seed);
highPos = (176 + Math.abs(random.nextInt(39)));
lowPos = 161 + Math.abs(random.nextInt(93));
byte[] b = new byte[2];
b[0] = (new Integer(highPos)).byteValue();
b[1] = (new Integer(lowPos)).byteValue();
str = new String(b, "GBK");
return str;
}
原理是根据汉字的区位码,其中本例高位从176位置取,低位从161位置开始取
public static String getChineseCharacter(long seed) throws Exception {
String str = null;
int highPos, lowPos;
Random random = new Random(seed);
highPos = (176 + Math.abs(random.nextInt(39)));
lowPos = 161 + Math.abs(random.nextInt(93));
byte[] b = new byte[2];
b[0] = (new Integer(highPos)).byteValue();
b[1] = (new Integer(lowPos)).byteValue();
str = new String(b, "GBK");
return str;
}
原理是根据汉字的区位码,其中本例高位从176位置取,低位从161位置开始取