JAVA字符串的各种编码转换

[java]  view plain copy
  1.      
  2. import java.io.UnsupportedEncodingException;  
  3.   
  4. /** 
  5.  * 转换字符串的编码 
  6.  */  
  7. public class ChangeCharset {  
  8.  /** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块 */  
  9.  public static final String US_ASCII = "US-ASCII";  
  10.   
  11.  /** ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 */  
  12.  public static final String ISO_8859_1 = "ISO-8859-1";  
  13.   
  14.  /** 8 位 UCS 转换格式 */  
  15.  public static final String UTF_8 = "UTF-8";  
  16.   
  17.  /** 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序 */  
  18.  public static final String UTF_16BE = "UTF-16BE";  
  19.   
  20.  /** 16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序 */  
  21.  public static final String UTF_16LE = "UTF-16LE";  
  22.   
  23.  /** 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识 */  
  24.  public static final String UTF_16 = "UTF-16";  
  25.   
  26.  /** 中文超大字符集 */  
  27.  public static final String GBK = "GBK";  
  28.   
  29.  /** 
  30.   * 将字符编码转换成US-ASCII码 
  31.   */  
  32.  public String toASCII(String str) throws UnsupportedEncodingException{  
  33.   return this.changeCharset(str, US_ASCII);  
  34.  }  
  35.  /** 
  36.   * 将字符编码转换成ISO-8859-1码 
  37.   */  
  38.  public String toISO_8859_1(String str) throws UnsupportedEncodingException{  
  39.   return this.changeCharset(str, ISO_8859_1);  
  40.  }  
  41.  /** 
  42.   * 将字符编码转换成UTF-8码 
  43.   */  
  44.  public String toUTF_8(String str) throws UnsupportedEncodingException{  
  45.   return this.changeCharset(str, UTF_8);  
  46.  }  
  47.  /** 
  48.   * 将字符编码转换成UTF-16BE码 
  49.   */  
  50.  public String toUTF_16BE(String str) throws UnsupportedEncodingException{  
  51.   return this.changeCharset(str, UTF_16BE);  
  52.  }  
  53.  /** 
  54.   * 将字符编码转换成UTF-16LE码 
  55.   */  
  56.  public String toUTF_16LE(String str) throws UnsupportedEncodingException{  
  57.   return this.changeCharset(str, UTF_16LE);  
  58.  }  
  59.  /** 
  60.   * 将字符编码转换成UTF-16码 
  61.   */  
  62.  public String toUTF_16(String str) throws UnsupportedEncodingException{  
  63.   return this.changeCharset(str, UTF_16);  
  64.  }  
  65.  /** 
  66.   * 将字符编码转换成GBK码 
  67.   */  
  68.  public String toGBK(String str) throws UnsupportedEncodingException{  
  69.   return this.changeCharset(str, GBK);  
  70.  }  
  71.    
  72.  /** 
  73.   * 字符串编码转换的实现方法 
  74.   * @param str  待转换编码的字符串 
  75.   * @param newCharset 目标编码 
  76.   * @return 
  77.   * @throws UnsupportedEncodingException 
  78.   */  
  79.  public String changeCharset(String str, String newCharset)  
  80.    throws UnsupportedEncodingException {  
  81.   if (str != null) {  
  82.    //用默认字符编码解码字符串。  
  83.    byte[] bs = str.getBytes();  
  84.    //用新的字符编码生成字符串  
  85.    return new String(bs, newCharset);  
  86.   }  
  87.   return null;  
  88.  }  
  89.  /** 
  90.   * 字符串编码转换的实现方法 
  91.   * @param str  待转换编码的字符串 
  92.   * @param oldCharset 原编码 
  93.   * @param newCharset 目标编码 
  94.   * @return 
  95.   * @throws UnsupportedEncodingException 
  96.   */  
  97.  public String changeCharset(String str, String oldCharset, String newCharset)  
  98.    throws UnsupportedEncodingException {  
  99.   if (str != null) {  
  100.    //用旧的字符编码解码字符串。解码可能会出现异常。  
  101.    byte[] bs = str.getBytes(oldCharset);  
  102.    //用新的字符编码生成字符串  
  103.    return new String(bs, newCharset);  
  104.   }  
  105.   return null;  
  106.  }  
  107.   
  108.  public static void main(String[] args) throws UnsupportedEncodingException {  
  109.   ChangeCharset test = new ChangeCharset();  
  110.   String str = "This is a 中文的 String!";  
  111.   System.out.println("str: " + str);  
  112.   String gbk = test.toGBK(str);  
  113.   System.out.println("转换成GBK码: " + gbk);  
  114.   System.out.println();  
  115.   String ascii = test.toASCII(str);  
  116.   System.out.println("转换成US-ASCII码: " + ascii);  
  117.   gbk = test.changeCharset(ascii,ChangeCharset.US_ASCII, ChangeCharset.GBK);  
  118.   System.out.println("再把ASCII码的字符串转换成GBK码: " + gbk);  
  119.   System.out.println();  
  120.   String iso88591 = test.toISO_8859_1(str);  
  121.   System.out.println("转换成ISO-8859-1码: " + iso88591);  
  122.   gbk = test.changeCharset(iso88591,ChangeCharset.ISO_8859_1, ChangeCharset.GBK);  
  123.   System.out.println("再把ISO-8859-1码的字符串转换成GBK码: " + gbk);  
  124.   System.out.println();  
  125.   String utf8 = test.toUTF_8(str);  
  126.   System.out.println("转换成UTF-8码: " + utf8);  
  127.   gbk = test.changeCharset(utf8,ChangeCharset.UTF_8, ChangeCharset.GBK);  
  128.   System.out.println("再把UTF-8码的字符串转换成GBK码: " + gbk);  
  129.   System.out.println();  
  130.   String utf16be = test.toUTF_16BE(str);  
  131.   System.out.println("转换成UTF-16BE码:" + utf16be);  
  132.   gbk = test.changeCharset(utf16be,ChangeCharset.UTF_16BE, ChangeCharset.GBK);  
  133.   System.out.println("再把UTF-16BE码的字符串转换成GBK码: " + gbk);  
  134.   System.out.println();  
  135.   String utf16le = test.toUTF_16LE(str);  
  136.   System.out.println("转换成UTF-16LE码:" + utf16le);  
  137.   gbk = test.changeCharset(utf16le,ChangeCharset.UTF_16LE, ChangeCharset.GBK);  
  138.   System.out.println("再把UTF-16LE码的字符串转换成GBK码: " + gbk);  
  139.   System.out.println();  
  140.   String utf16 = test.toUTF_16(str);  
  141.   System.out.println("转换成UTF-16码:" + utf16);  
  142.   gbk = test.changeCharset(utf16,ChangeCharset.UTF_16LE, ChangeCharset.GBK);  
  143.   System.out.println("再把UTF-16码的字符串转换成GBK码: " + gbk);  
  144.   String s = new String("中文".getBytes("UTF-8"),"UTF-8");  
  145.   System.out.println(s);  
  146.  }  
  147. }  


------------------------------------------------------------------------------------------------------------------


        java中的String类是按照unicode进行编码的,当使用String(byte[] bytes, String encoding)构造字符串时,encoding所指的是bytes中的数据是按照那种方式编码的,而不是最后产生的String是什么编码方式,换句话说,是让系统把bytes中的数据由encoding编码方式转换成unicode编码。如果不指明,bytes的编码方式将由jdk根据操作系统决定。

        当我们从文件中读数据时,最好使用InputStream方式,然后采用String(byte[] bytes, String encoding)指明文件的编码方式。不要使用Reader方式,因为Reader方式会自动根据jdk指明的编码方式把文件内容转换成unicode编码。

        当我们从数据库中读文本数据时,采用ResultSet.getBytes()方法取得字节数组,同样采用带编码方式的字符串构造方法即可。

ResultSet rs;
bytep[] bytes = rs.getBytes();
String str = new String(bytes, "gb2312");

不要采取下面的步骤。

ResultSet rs;
String str = rs.getString();
str = new String(str.getBytes("iso8859-1"), "gb2312");

        这种编码转换方式效率底。之所以这么做的原因是,ResultSet在getString()方法执行时,默认数据库里的数据编码方式为iso8859-1。系统会把数据依照iso8859-1的编码方式转换成unicode。使用str.getBytes("iso8859-1")把数据还原,然后利用new String(bytes, "gb2312")把数据从gb2312转换成unicode,中间多了好多步骤。

        从HttpRequest中读参数时,利用reqeust.setCharacterEncoding()方法设置编码方式,读出的内容就是正确的了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值