当前位置: 移动技术网 > IT编程>开发语言>Java > JAVA 中实现整句汉字拆分、转换为ASCII实例详解

JAVA 中实现整句汉字拆分、转换为ASCII实例详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

java 中实现整句汉字拆分、转换为ascii实例详解

大家都知道,一个汉字等于两个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)

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网