無論是對程序的本地化還是國際化,都會涉及到字符編碼的轉換的問題。尤其在web應用中常常需要處理中文字符,這時就需要進行字符串的編碼轉換,將字符串編碼轉換為GBK或者GB2312。
一、關鍵技術點: 1、當前流行的字符編碼格式有:US-ASCII、ISO-8859-1、UTF-8、UTF-16BE、UTF-16LE、UTF-16、GBK、GB2312等,其中GBK、GB2312是專門處理中文編碼的。 2、String的getBytes方法用于按指定編碼獲取字符串的字節(jié)數(shù)組,參數(shù)指定了解碼格式,如果沒有指定解碼格式,則按系統(tǒng)默認編碼格式。 3、String的“String(bytes[] bs, String charset)”構造方法用于把字節(jié)數(shù)組按指定的格式組合成一個字符串對象 二、實例演示: package book.String; import java.io.UnsupportedEncodingException; /** * 轉換字符串的編碼 * @author joe * */ public class ChangeCharset { /** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁塊 */ public static final String US_ASCII = "US-ASCII"; /** ISO拉丁字母表 No.1,也叫做ISO-LATIN-1 */ public static final String ISO_8859_1 = "ISO-8859-1"; /** 8 位 UCS 轉換格式 */ public static final String UTF_8 = "UTF-8"; /** 16 位 UCS 轉換格式,Big Endian(最低地址存放高位字節(jié))字節(jié)順序 */ public static final String UTF_16BE = "UTF-16BE"; /** 16 位 UCS 轉換格式,Litter Endian(最高地址存放地位字節(jié))字節(jié)順序 */ public static final String UTF_16LE = "UTF-16LE"; /** 16 位 UCS 轉換格式,字節(jié)順序由可選的字節(jié)順序標記來標識 */ public static final String UTF_16 = "UTF-16"; /** 中文超大字符集 **/ public static final String GBK = "GBK"; public static final String GB2312 = "GB2312"; /** 將字符編碼轉換成US-ASCII碼 */ public String toASCII(String str) throws UnsupportedEncodingException { return this.changeCharset(str, US_ASCII); } /** 將字符編碼轉換成ISO-8859-1 */ public String toISO_8859_1(String str) throws UnsupportedEncodingException { return this.changeCharset(str, ISO_8859_1); } /** 將字符編碼轉換成UTF-8 */ public String toUTF_8(String str) throws UnsupportedEncodingException { return this.changeCharset(str, UTF_8); } /** 將字符編碼轉換成UTF-16BE */ public String toUTF_16BE(String str) throws UnsupportedEncodingException{ return this.changeCharset(str, UTF_16BE); } /** 將字符編碼轉換成UTF-16LE */ public String toUTF_16LE(String str) throws UnsupportedEncodingException { return this.changeCharset(str, UTF_16LE); } /** 將字符編碼轉換成UTF-16 */ public String toUTF_16(String str) throws UnsupportedEncodingException { return this.changeCharset(str, UTF_16); } /** 將字符編碼轉換成GBK */ public String toGBK(String str) throws UnsupportedEncodingException { return this.changeCharset(str, GBK); } /** 將字符編碼轉換成GB2312 */ public String toGB2312(String str) throws UnsupportedEncodingException { return this.changeCharset(str,GB2312); } /** * 字符串編碼轉換的實現(xiàn)方法 * @param str 待轉換的字符串 * @param newCharset 目標編碼 */ public String changeCharset(String str, String newCharset) throws UnsupportedEncodingException { if(str != null) { //用默認字符編碼解碼字符串。與系統(tǒng)相關,中文windows默認為GB2312 byte[] bs = str.getBytes(); return new String(bs, newCharset); //用新的字符編碼生成字符串 } return null; } /** * 字符串編碼轉換的實現(xiàn)方法 * 注:該函數(shù)效率較低(不建議使用)
* @param str 待轉換的字符串 * @param oldCharset 源字符集 * @param newCharset 目標字符集 */ public String changeCharset(String str, String oldCharset, String newCharset) throws UnsupportedEncodingException { if(str != null) { //用源字符編碼解碼字符串 byte[] bs = str.getBytes(oldCharset); return new String(bs, newCharset); } return null; } public static void main(String[] args) throws UnsupportedEncodingException { ChangeCharset test = new ChangeCharset(); String str = "This is a 中文的 String!"; System.out.println("str:" + str); String gbk = test.toGBK(str); System.out.println("轉換成GBK碼:" + gbk); System.out.println(); String ascii = test.toASCII(str); System.out.println("轉換成US-ASCII:" + ascii); System.out.println(); String iso88591 = test.toISO_8859_1(str); System.out.println("轉換成ISO-8859-1碼:" + iso88591); System.out.println(); gbk = test.changeCharset(iso88591, ISO_8859_1, GBK); System.out.println("再把ISO-8859-1碼的字符串轉換成GBK碼:" + gbk); System.out.println(); String utf8 = test.toUTF_8(str); System.out.println(); System.out.println("轉換成UTF-8碼:" + utf8); String utf16be = test.toUTF_16BE(str); System.out.println("轉換成UTF-16BE碼:" + utf16be); gbk = test.changeCharset(utf16be, UTF_16BE, GBK); System.out.println("再把UTF-16BE編碼的字符轉換成GBK碼:" + gbk); System.out.println(); String utf16le = test.toUTF_16LE(str); System.out.println("轉換成UTF-16LE碼:" + utf16le); gbk = test.changeCharset(utf16le, UTF_16LE, GBK); System.out.println("再把UTF-16LE編碼的字符串轉換成GBK碼:" + gbk); System.out.println(); String utf16 = test.toUTF_16(str); System.out.println("轉換成UTF-16碼:" + utf16); String gb2312 = test.changeCharset(utf16, UTF_16, GB2312); System.out.println("再把UTF-16編碼的字符串轉換成GB2312碼:" + gb2312); } } str:This is a 中文的 String! 轉換成GBK碼:This is a 中文的 String! 轉換成US-ASCII:This is a ?????? String! 轉換成ISO-8859-1碼:This is a ?????? String! 再把ISO-8859-1碼的字符串轉換成GBK碼:This is a 中文的 String! 轉換成UTF-8碼:This is a ????? String! 轉換成UTF-16BE碼:周楳?猠慍????瑲楮朡 再把UTF-16BE編碼的字符轉換成GBK碼:This is a 中文的 String! 轉換成UTF-16LE碼:桔獩槧?????匠牴湩Ⅷ 再把UTF-16LE編碼的字符串轉換成GBK碼:This is a 中文的 String! 轉換成UTF-16碼:周楳?猠慍????瑲楮朡 再把UTF-16編碼的字符串轉換成GB2312碼:?This is a 中文的 String! 三、源碼分析: 更改字符串編碼的步驟為: 1、調用String的getByte方法對字符串進行解碼,得到字符串的字節(jié)數(shù)組(字節(jié)數(shù)組不攜帶任何有關編碼格式的信息,只有字符才有編碼格式) 2、根據(jù)字節(jié)數(shù)組和新的字符編碼構造一個新的String對象,得到的就是按照新的字符編碼生成的字符串 【說明】
java中的String類是按照unicode進行編碼的,當使用String(byte[] bytes, String encoding)構造字符串時,encoding所指的是bytes中的數(shù)據(jù)是按照那種方式編碼的,而不是最后產(chǎn)生的String是什么編碼方式,換句話說,是讓系統(tǒng)把bytes中的數(shù)據(jù)由encoding編碼方式轉換成unicode編碼。如果不指明,bytes的編碼方式將由jdk根據(jù)操作系統(tǒng)決定。
當我們從文件中讀數(shù)據(jù)時,最好使用InputStream方式,然后采用String(byte[] bytes, String encoding)指明文件的編碼方式。不要使用Reader方式,因為Reader方式會自動根據(jù)jdk指明的編碼方式把文件內容轉換成unicode編碼。 當我們從數(shù)據(jù)庫中讀文本數(shù)據(jù)時,采用ResultSet.getBytes()方法取得字節(jié)數(shù)組,同樣采用帶編碼方式的字符串構造方法即可。 ResultSet rs; 不要采取下面的步驟。 ResultSet rs; 這種編碼轉換方式效率底。之所以這么做的原因是,ResultSet在getString()方法執(zhí)行時,默認數(shù)據(jù)庫里的數(shù)據(jù)編碼方式為iso8859-1。系統(tǒng)會把數(shù)據(jù)依照iso8859-1的編碼方式轉換成unicode。使用str.getBytes("iso8859-1")把數(shù)據(jù)還原,然后利用new String(bytes, "gb2312")把數(shù)據(jù)從gb2312轉換成unicode,中間多了好多步驟。 從HttpRequest中讀參數(shù)時,利用reqeust.setCharacterEncoding()方法設置編碼方式,讀出的內容就是正確的了。 順便說句,如果你直接在瀏覽器中的地址欄輸入這個的話,瀏覽器都會使用ISO-8859-1來編碼你的URL,然后才提交到服務器,這也是為啥一般服務器端都需要首先進行編碼轉換。 |
|