📄 characode.java
字号:
package ECDictionaryUtil;
public class CharaCode {
/**
* @param args
*/
public int getCharaCode(String chara)throws Exception
{ byte[] bytes = chara.getBytes("gbk");
if(bytes == null || bytes.length > 2 || bytes.length <= 0|| bytes.length == 1)
{ throw new Exception("不是一个有效的汉字!"); }
if(bytes.length == 2){ //汉字
int hByte,lByte;//汉字的高字节和低字节
if (bytes[0]<0)
{
hByte = 256 + bytes[0];
}
else{
//hByte = bytes[0];
throw new Exception("不是一个有效的汉字!");
}
if (bytes[1]<0)
{
lByte= 256 + bytes[1];
}
else{
//lByte = bytes[1];
throw new Exception("不是一个有效的汉字!");
}
int iHanzi = (256 * hByte + lByte);
return iHanzi;
}
throw new Exception("不是一个有效的汉字!");
}
public static void main(String[] args) {
String cha="华";
try{
System.out.println(new CharaCode().getCharaCode(cha));
}catch(Exception ee){
System.out.println(ee.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -