字符串编码的转换

 1 package com.jdk7.chapter5;
 2 
 3 import java.io.UnsupportedEncodingException;
 4 
 5 public class ChangeCharsetTest {
 6     //公共的唯一静态变量
 7     public static final String US_ASCII = "US-ASCII";
 8     public static final String ISO_8859_1 = "ISO-8859-1";
 9     public static final String UTF_8 = "UTF-8";
10     public static final String UTF_16BE = "UTF-16BE";
11     public static final String UTF_16LE = "UTF-16LE";
12     public static final String UTF_16 = "UTF-16";
13     public static final String GBK = "GBK";
14     
15     public String toUS_ASCII(String str) throws UnsupportedEncodingException{
16         return changeCharset(str, this.US_ASCII);
17     }
18     
19     public String toISO_8859(String str) throws UnsupportedEncodingException{
20         return changeCharset(str, this.ISO_8859_1);
21     }
22     
23     public String toUTF_8(String str) throws UnsupportedEncodingException{
24         return changeCharset(str, this.UTF_8);
25     }
26     
27     public String toUTF_16BE(String str) throws UnsupportedEncodingException{
28         return changeCharset(str, this.UTF_16BE);
29     }
30     
31     public String toUTF_16LE(String str) throws UnsupportedEncodingException{
32         return changeCharset(str, this.UTF_16LE);
33     }
34     
35     public String toUTF_16(String str) throws UnsupportedEncodingException{
36         return changeCharset(str, this.UTF_16);
37     }
38     
39     public String toGBK(String str) throws UnsupportedEncodingException{
40         return changeCharset(str, this.GBK);
41     }
42     
43     public String changeCharset(String str, String newCharset) throws UnsupportedEncodingException{
44         byte[] bt = str.getBytes();
45         return new String(bt, newCharset);
46     }
47     
48     public String changeCharset(String str, String newCharset, String oldCharset) throws UnsupportedEncodingException{
49         byte[] bt = str.getBytes(oldCharset);
50         return new String(bt, newCharset);
51     }
52     
53     public static void main(String[] args) throws UnsupportedEncodingException {
54         ChangeCharsetTest cct = new ChangeCharsetTest();
55         String str = "This is 中文 charset!";
56         System.out.println("cct.toUS_ASCII("+str+")>"+cct.toUS_ASCII(str));
57         System.out.println("cct.toGBK("+str+")>"+cct.toGBK(str));
58         System.out.println("cct.toISO_8859("+str+")>"+cct.toISO_8859(str));
59         System.out.println("cct.toUTF_16("+str+")>"+cct.toUTF_16(str));
60         System.out.println("cct.toUTF_16BE("+str+")>"+cct.toUTF_16BE(str));
61         System.out.println("cct.toUTF_16LE("+str+")>"+cct.toUTF_16LE(str));
62         System.out.println("cct.toUTF_8("+str+")>"+cct.toUTF_8(str));
63         System.out.println("GBK to ISO_8859_1>"+cct.changeCharset(str, GBK, ISO_8859_1));
64         System.out.println("GBK to US_ASCII>"+cct.changeCharset(str, GBK, US_ASCII));
65         System.out.println("GBK to UTF_16>"+cct.changeCharset(str, GBK, UTF_16));
66         System.out.println("GBK to UTF_16BE>"+cct.changeCharset(str, GBK, UTF_16BE));
67         System.out.println("GBK to UTF_16LE>"+cct.changeCharset(str, GBK, UTF_16LE));
68         System.out.println("GBK to UTF_8>"+cct.changeCharset(str, GBK, UTF_8));
69     }
70 }
71 
72 执行结果:
73 cct.toUS_ASCII(This is 中文 charset!)>This is ???? charset!
74 cct.toGBK(This is 中文 charset!)>This is 中文 charset!
75 cct.toISO_8859(This is 中文 charset!)>This is ???? charset!
76 cct.toUTF_16(This is 中文 charset!)>周楳?猠???桡牳整?
77 cct.toUTF_16BE(This is 中文 charset!)>周楳?猠???桡牳整?
78 cct.toUTF_16LE(This is 中文 charset!)>桔獩椠???挠慨獲瑥?
79 cct.toUTF_8(This is 中文 charset!)>This is ???? charset!
80 GBK to ISO_8859_1>This is ?? charset!
81 GBK to US_ASCII>This is ?? charset!
82 GBK to UTF_16>?

 

转载于:https://www.cnblogs.com/celine/p/8463626.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值