字符串编码的转换

Posted celineluo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串编码的转换相关的知识,希望对你有一定的参考价值。

 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>?

 

以上是关于字符串编码的转换的主要内容,如果未能解决你的问题,请参考以下文章

如何测试文本片段是不是是 Quoted-printable 编码的

10个JavaScript代码片段,使你更加容易前端开发。

10个JavaScript代码片段,使你更加容易前端开发。

linux编码转换问题

Qt代码区字符编码转换

web代码片段