JAVA轉換字符編碼

 

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章