characode.java

来自「java树型字典的建立,是中文分词 检索的基础」· Java 代码 · 共 48 行

JAVA
48
字号
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 + =
减小字号Ctrl + -
显示快捷键?