本文来自:http://blog.youkuaiyun.com/hellogv/ ,转载必须注明出处!
大家都知道,一个汉字等于两个byte的大小。二进制数据通过网络传输时,如果两个byte都超过128则会合并成一个Unicode(汉字)字符,本文的代码主要实现的功能是:把这些汉字拆分为byte,然后重新变为ASCII类型的字符串。
结果是:
compile:
run:
10
???????????ù??°????? ASCII字符如果超过128,则会显示为?,但是其本身的值不变
20
BUILD SUCCESSFUL (total time: 1 second)
大家都知道,一个汉字等于两个byte的大小。二进制数据通过网络传输时,如果两个byte都超过128则会合并成一个Unicode(汉字)字符,本文的代码主要实现的功能是:把这些汉字拆分为byte,然后重新变为ASCII类型的字符串。
- public static String ChineseToASCII(byte[] rec) { //从字节读取内容
- ByteArrayInputStream bais = new ByteArrayInputStream(rec);
- DataInputStream dis = new DataInputStream(bais);
- String BTS=null;
- try {
- BTS=new String(rec,"ISO8859-1");//转换编码
- bais.close();
- dis.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return BTS;
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- String source="一二三四五六七八九十";
- System.out.println(source.length());
- String target=ChineseToASCII(source.getBytes());
- System.out.println(target);
- System.out.println(target.length());
- }
compile:
run:
10
???????????ù??°????? ASCII字符如果超过128,则会显示为?,但是其本身的值不变
20
BUILD SUCCESSFUL (total time: 1 second)