工具代码总结

2015-03-21  本文已影响37人  风语安然
   //单个汉字转成ansi
public static int getChsAscii(String chs){
    int asc=0;
    
    try {
        byte[] bytes=chs.getBytes("gb2312");
        
        if(bytes==null||bytes.length==0){
            throw new RuntimeException("illegal resource string");
        }
        
        if(bytes.length==1){    //英文字符
            asc=bytes[0];
        }
        
        if(bytes.length==2){    //中文字符
            int highByte=256+bytes[0];
            int lowByte=256+bytes[1];
            
            asc=(256*highByte+lowByte)-256*256; 
        }
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    return asc;
}
上一篇 下一篇

猜你喜欢

热点阅读