001 | package com.zuidaima.util; |
002 |
003 | import java.io.UnsupportedEncodingException; |
004 |
005 | /** |
006 | * 转换字符串的编码 |
007 | */ |
008 | public class ChangeCharset { |
009 | /** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块 */ |
010 | public static final String US_ASCII = "US-ASCII" ; |
011 |
012 | /** ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 */ |
013 | public static final String ISO_8859_1 = "ISO-8859-1" ; |
014 |
015 | /** 8 位 UCS 转换格式 */ |
016 | public static final String UTF_8 = "UTF-8" ; |
017 |
018 | /** 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序 */ |
019 | public static final String UTF_16BE = "UTF-16BE" ; |
020 |
021 | /** 16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序 */ |
022 | public static final String UTF_16LE = "UTF-16LE" ; |
023 |
024 | /** 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识 */ |
025 | public static final String UTF_16 = "UTF-16" ; |
026 |
027 | /** 中文超大字符集 */ |
028 | public static final String GBK = "GBK" ; |
029 |
030 | /** |
031 | * 将字符编码转换成US-ASCII码 |
032 | */ |
033 | public String toASCII(String str) throws UnsupportedEncodingException { |
034 | return this .changeCharset(str, US_ASCII); |
035 | } |
036 |
037 | /** |
038 | * 将字符编码转换成ISO-8859-1码 |
039 | */ |
040 | public String toISO_8859_1(String str) throws UnsupportedEncodingException { |
041 | return this .changeCharset(str, ISO_8859_1); |
042 | } |
043 |
044 | /** |
045 | * 将字符编码转换成UTF-8码 |
046 | */ |
047 | public String toUTF_8(String str) throws UnsupportedEncodingException { |
048 | return this .changeCharset(str, UTF_8); |
049 | } |
050 |
051 | /** |
052 | * 将字符编码转换成UTF-16BE码 |
053 | */ |
054 | public String toUTF_16BE(String str) throws UnsupportedEncodingException { |
055 | return this .changeCharset(str, UTF_16BE); |
056 | } |
057 |
058 | /** |
059 | * 将字符编码转换成UTF-16LE码 |
060 | */ |
061 | public String toUTF_16LE(String str) throws UnsupportedEncodingException { |
062 | return this .changeCharset(str, UTF_16LE); |
063 | } |
064 |
065 | /** |
066 | * 将字符编码转换成UTF-16码 |
067 | */ |
068 | public String toUTF_16(String str) throws UnsupportedEncodingException { |
069 | return this .changeCharset(str, UTF_16); |
070 | } |
071 |
072 | /** |
073 | * 将字符编码转换成GBK码 |
074 | */ |
075 | public String toGBK(String str) throws UnsupportedEncodingException { |
076 | return this .changeCharset(str, GBK); |
077 | } |
078 |
079 | /** |
080 | * 字符串编码转换的实现方法 |
081 | * |
082 | * @param str |
083 | * 待转换编码的字符串 |
084 | * @param newCharset |
085 | * 目标编码 |
086 | * @return |
087 | * @throws UnsupportedEncodingException |
088 | */ |
089 | public String changeCharset(String str, String newCharset) |
090 | throws UnsupportedEncodingException { |
091 | if (str != null ) { |
092 | // 用默认字符编码解码字符串。 |
093 | byte [] bs = str.getBytes(); |
094 | // 用新的字符编码生成字符串 |
095 | return new String(bs, newCharset); |
096 | } |
097 | return null ; |
098 | } |
099 |
100 | /** |
101 | * 字符串编码转换的实现方法 |
102 | * |
103 | * @param str |
104 | * 待转换编码的字符串 |
105 | * @param oldCharset |
106 | * 原编码 |
107 | * @param newCharset |
108 | * 目标编码 |
109 | * @return |
110 | * @throws UnsupportedEncodingException |
111 | */ |
112 | public String changeCharset(String str, String oldCharset, String newCharset) |
113 | throws UnsupportedEncodingException { |
114 | if (str != null ) { |
115 | // 用旧的字符编码解码字符串。解码可能会出现异常。 |
116 | byte [] bs = str.getBytes(oldCharset); |
117 | // 用新的字符编码生成字符串 |
118 | return new String(bs, newCharset); |
119 | } |
120 | return null ; |
121 | } |
122 |
123 | public static void main(String[] args) throws UnsupportedEncodingException { |
124 | ChangeCharset test = new ChangeCharset(); |
125 | String str = "This is a 中文的 String!" ; |
126 | System.out.println( "str: " + str); |
127 | String gbk = test.toGBK(str); |
128 | System.out.println( "转换成GBK码: " + gbk); |
129 | System.out.println(); |
130 | String ascii = test.toASCII(str); |
131 | System.out.println( "转换成US-ASCII码: " + ascii); |
132 | gbk = test.changeCharset(ascii, ChangeCharset.US_ASCII, |
133 | ChangeCharset.GBK); |
134 | System.out.println( "再把ASCII码的字符串转换成GBK码: " + gbk); |
135 | System.out.println(); |
136 | String iso88591 = test.toISO_8859_1(str); |
137 | System.out.println( "转换成ISO-8859-1码: " + iso88591); |
138 | gbk = test.changeCharset(iso88591, ChangeCharset.ISO_8859_1, |
139 | ChangeCharset.GBK); |
140 | System.out.println( "再把ISO-8859-1码的字符串转换成GBK码: " + gbk); |
141 | System.out.println(); |
142 | String utf8 = test.toUTF_8(str); |
143 | System.out.println( "转换成UTF-8码: " + utf8); |
144 | gbk = test.changeCharset(utf8, ChangeCharset.UTF_8, ChangeCharset.GBK); |
145 | System.out.println( "再把UTF-8码的字符串转换成GBK码: " + gbk); |
146 | System.out.println(); |
147 | String utf16be = test.toUTF_16BE(str); |
148 | System.out.println( "转换成UTF-16BE码:" + utf16be); |
149 | gbk = test.changeCharset(utf16be, ChangeCharset.UTF_16BE, |
150 | ChangeCharset.GBK); |
151 | System.out.println( "再把UTF-16BE码的字符串转换成GBK码: " + gbk); |
152 | System.out.println(); |
153 | String utf16le = test.toUTF_16LE(str); |
154 | System.out.println( "转换成UTF-16LE码:" + utf16le); |
155 | gbk = test.changeCharset(utf16le, ChangeCharset.UTF_16LE, |
156 | ChangeCharset.GBK); |
157 | System.out.println( "再把UTF-16LE码的字符串转换成GBK码: " + gbk); |
158 | System.out.println(); |
159 | String utf16 = test.toUTF_16(str); |
160 | System.out.println( "转换成UTF-16码:" + utf16); |
161 | gbk = test.changeCharset(utf16, ChangeCharset.UTF_16LE, |
162 | ChangeCharset.GBK); |
163 | System.out.println( "再把UTF-16码的字符串转换成GBK码: " + gbk); |
164 | String s = new String( "中文" .getBytes( "UTF-8" ), "UTF-8" ); |
165 | System.out.println(s); |
166 | } |
167 | } |
输出结果:
001 | package com.zuidaima.util; |
002 |
003 | import java.io.UnsupportedEncodingException; |
004 |
005 | /** |
006 | * 转换字符串的编码 |
007 | */ |
008 | public class ChangeCharset { |
009 | /** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块 */ |
010 | public static final String US_ASCII = "US-ASCII" ; |
011 |
012 | /** ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 */ |
013 | public static final String ISO_8859_1 = "ISO-8859-1" ; |
014 |
015 | /** 8 位 UCS 转换格式 */ |
016 | public static final String UTF_8 = "UTF-8" ; |
017 |
018 | /** 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序 */ |
019 | public static final String UTF_16BE = "UTF-16BE" ; |
020 |
021 | /** 16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序 */ |
022 | public static final String UTF_16LE = "UTF-16LE" ; |
023 |
024 | /** 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识 */ |
025 | public static final String UTF_16 = "UTF-16" ; |
026 |
027 | /** 中文超大字符集 */ |
028 | public static final String GBK = "GBK" ; |
029 |
030 | /** |
031 | * 将字符编码转换成US-ASCII码 |
032 | */ |
033 | public String toASCII(String str) throws UnsupportedEncodingException { |
034 | return this .changeCharset(str, US_ASCII); |
035 | } |
036 |
037 | /** |
038 | * 将字符编码转换成ISO-8859-1码 |
039 | */ |
040 | public String toISO_8859_1(String str) throws UnsupportedEncodingException { |
041 | return this .changeCharset(str, ISO_8859_1); |
042 | } |
043 |
044 | /** |
045 | * 将字符编码转换成UTF-8码 |
046 | */ |
047 | public String toUTF_8(String str) throws UnsupportedEncodingException { |
048 | return this .changeCharset(str, UTF_8); |
049 | } |
050 |
051 | /** |
052 | * 将字符编码转换成UTF-16BE码 |
053 | */ |
054 | public String toUTF_16BE(String str) throws UnsupportedEncodingException { |
055 | return this .changeCharset(str, UTF_16BE); |
056 | } |
057 |
058 | /** |
059 | * 将字符编码转换成UTF-16LE码 |
060 | */ |
061 | public String toUTF_16LE(String str) throws UnsupportedEncodingException { |
062 | return this .changeCharset(str, UTF_16LE); |
063 | } |
064 |
065 | /** |
066 | * 将字符编码转换成UTF-16码 |
067 | */ |
068 | public String toUTF_16(String str) throws UnsupportedEncodingException { |
069 | return this .changeCharset(str, UTF_16); |
070 | } |
071 |
072 | /** |
073 | * 将字符编码转换成GBK码 |
074 | */ |
075 | public String toGBK(String str) throws UnsupportedEncodingException { |
076 | return this .changeCharset(str, GBK); |
077 | } |
078 |
079 | /** |
080 | * 字符串编码转换的实现方法 |
081 | * |
082 | * @param str |
083 | * 待转换编码的字符串 |
084 | * @param newCharset |
085 | * 目标编码 |
086 | * @return |
087 | * @throws UnsupportedEncodingException |
088 | */ |
089 | public String changeCharset(String str, String newCharset) |
090 | throws UnsupportedEncodingException { |
091 | if (str != null ) { |
092 | // 用默认字符编码解码字符串。 |
093 | byte [] bs = str.getBytes(); |
094 | // 用新的字符编码生成字符串 |
095 | return new String(bs, newCharset); |
096 | } |
097 | return null ; |
098 | } |
099 |
100 | /** |
101 | * 字符串编码转换的实现方法 |
102 | * |
103 | * @param str |
104 | * 待转换编码的字符串 |
105 | * @param oldCharset |
106 | * 原编码 |
107 | * @param newCharset |
108 | * 目标编码 |
109 | * @return |
110 | * @throws UnsupportedEncodingException |
111 | */ |
112 | public String changeCharset(String str, String oldCharset, String newCharset) |
113 | throws UnsupportedEncodingException { |
114 | if (str != null ) { |
115 | // 用旧的字符编码解码字符串。解码可能会出现异常。 |
116 | byte [] bs = str.getBytes(oldCharset); |
117 | // 用新的字符编码生成字符串 |
118 | return new String(bs, newCharset); |
119 | } |
120 | return null ; |
121 | } |
122 |
123 | public static void main(String[] args) throws UnsupportedEncodingException { |
124 | ChangeCharset test = new ChangeCharset(); |
125 | String str = "This is a 中文的 String!" ; |
126 | System.out.println( "str: " + str); |
127 | String gbk = test.toGBK(str); |
128 | System.out.println( "转换成GBK码: " + gbk); |
129 | System.out.println(); |
130 | String ascii = test.toASCII(str); |
131 | System.out.println( "转换成US-ASCII码: " + ascii); |
132 | gbk = test.changeCharset(ascii, ChangeCharset.US_ASCII, |
133 | ChangeCharset.GBK); |
134 | System.out.println( "再把ASCII码的字符串转换成GBK码: " + gbk); |
135 | System.out.println(); |
136 | String iso88591 = test.toISO_8859_1(str); |
137 | System.out.println( "转换成ISO-8859-1码: " + iso88591); |
138 | gbk = test.changeCharset(iso88591, ChangeCharset.ISO_8859_1, |
139 | ChangeCharset.GBK); |
140 | System.out.println( "再把ISO-8859-1码的字符串转换成GBK码: " + gbk); |
141 | System.out.println(); |
142 | String utf8 = test.toUTF_8(str); |
143 | System.out.println( "转换成UTF-8码: " + utf8); |
144 | gbk = test.changeCharset(utf8, ChangeCharset.UTF_8, ChangeCharset.GBK); |
145 | System.out.println( "再把UTF-8码的字符串转换成GBK码: " + gbk); |
146 | System.out.println(); |
147 | String utf16be = test.toUTF_16BE(str); |
148 | System.out.println( "转换成UTF-16BE码:" + utf16be); |
149 | gbk = test.changeCharset(utf16be, ChangeCharset.UTF_16BE, |
150 | ChangeCharset.GBK); |
151 | System.out.println( "再把UTF-16BE码的字符串转换成GBK码: " + gbk); |
152 | System.out.println(); |
153 | String utf16le = test.toUTF_16LE(str); |
154 | System.out.println( "转换成UTF-16LE码:" + utf16le); |
155 | gbk = test.changeCharset(utf16le, ChangeCharset.UTF_16LE, |
156 | ChangeCharset.GBK); |
157 | System.out.println( "再把UTF-16LE码的字符串转换成GBK码: " + gbk); |
158 | System.out.println(); |
159 | String utf16 = test.toUTF_16(str); |
160 | System.out.println( "转换成UTF-16码:" + utf16); |
161 | gbk = test.changeCharset(utf16, ChangeCharset.UTF_16LE, |
162 | ChangeCharset.GBK); |
163 | System.out.println( "再把UTF-16码的字符串转换成GBK码: " + gbk); |
164 | String s = new String( "中文" .getBytes( "UTF-8" ), "UTF-8" ); |
165 | System.out.println(s); |
166 | } |
167 | } |
输出结果: