歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java 字符轉碼之UTF-8轉為GBK/GB2312

Java 字符轉碼之UTF-8轉為GBK/GB2312

日期:2017/3/1 10:23:19   编辑:Linux編程

定義一字符串:

  1. String t = "hfjkds中國中國中國中國中國中國中國中國中國中國中國中國中國中國中國中國中國中國中國中國hfsdkj<img src='sasa' /> fjldsajflkdsjaflkdsjalf <img src='sada' ait=''/>sfdsfadas";
思路:先轉為Unicode,然後轉為GBK
  1. String utf8 = new String(t.getBytes( "UTF-8"));
  2. System.out.println(utf8);
  3. String unicode = new String(utf8.getBytes(),"UTF-8");
  4. System.out.println(unicode);
  5. String gbk = new String(unicode.getBytes("GBK"));
  6. System.out.println(gbk);
Copyright © Linux教程網 All Rights Reserved