文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>JAVA转换字符编码

JAVA转换字符编码

时间:2010-09-19  来源:zhd070341401

 

  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. }  

 

排行榜 更多 +
零界之痕手游安卓下载

零界之痕手游安卓下载

角色扮演 下载
漫游都市手机版下载

漫游都市手机版下载

赛车竞速 下载
涡轮螺旋桨飞行模拟器无限金币版下载

涡轮螺旋桨飞行模拟器无限金币版下载

模拟经营 下载