汉字区位码:
位码范围:区码为55时,位码范围为1-89;其他区码,位码范围为1-94。区码范围:01-09区为682个特殊字符,16~87区为汉字区,包含6763个汉字 。其中16-55区为一级汉字(3755个最常用的汉字,按拼音字母的次序排列),56-87区为二级汉字(3008个汉字,按部首次序排列)。
随机生成中文名字
import java.io.UnsupportedEncodingException;
public class ChineseName {
public static void main(String[] args) {
ChineseName d = new ChineseName();
System.out.println(d.getName());//输出随机的中文名字
}
//获得汉字名字
public String getName(){
String name = "";
int chineseNameNum = (int)(Math.random()*3 + 2);
for(int i=1; i<=chineseNameNum;i++){
name += this.getChinese();
}
return name;
}
//获得单个汉字
public String getChinese(){
String chinese = "";
int i = (int)(Math.random()*40 + 16);
int j = (int)(Math.random()*94 + 1);
if(i == 55){
j = (int)(Math.random()*89 + 1);
}
byte[] bytes = {(byte) (i+160),(byte) (j+160)};
try {
chinese = new String(bytes, "gb2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return chinese;
}
}
将单个汉字转换为区位码:
import java.io.UnsupportedEncodingException;
public class ChineseToCode {
public static void main(String[] args) {
ChineseToCode ctc = new ChineseToCode();
System.out.println(ctc.getCode("王"));
}
public String getCode(String chinese){
byte[] bt = null;
try {
bt = chinese.getBytes("gb2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String code = "";
for(int i = 0;i<bt.length;i++){
String hex = Integer.toHexString(bt[i] & 0xFF);// 转换为16进制
if (hex.length() == 1) {
hex = '0' + hex; // 长度是1位的,在前面补0
}
hex = hex.toUpperCase(); // 转换为大写
int a = Integer.parseInt(hex,16);
code += (a - 0x80 - 0x20) + ""; // 获得区位码
}
return code;
}
}