`
wangyanlong0107
  • 浏览: 481996 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

【转】java 与 iso-8859-1 编码

 
阅读更多

今天又研究了一下字符编码。写了些测试代码,算是比较了解了。 

主要是研究 iso-8859-1 
建议先看这篇:《第二篇:JAVA字符编码系列二:Unicode,ISO-8859-1,GBK,UTF-8编码及...》 
http://my.oschina.net/whp/blog/36846 

然后再看看 iso-8859-1 的定义。 
http://baike.baidu.com/view/2613676.htm 


这一句话非常关键: 
因为ISO-8859-1编码范围使用了单字节内的所有空间,在支持ISO-8859-1的系统中传输和存储其他任何编码的字节流都不会被抛弃。换言之,把其他任何编码的字节流当作ISO-8859-1编码看待都没有问题。这是个很重要的特性,MySQL数据库默认编码是Latin1就是利用了这个特性。 

在工作中,从 web 提交的数据,到了 web 容器后,变成了 iso-8859-1 的字符串。 
可以认为,被执行了如下代码: 
byte[] buf_gbk = s.getBytes("gbk"); 
String sIso88591 = new String(buf_gbk, "iso-8859-1"); 

所以,我们获取数据时需要执行: 
byte[] buf_iso88591 = sIso88591.getBytes("iso-8859-1"); 
String sGbk = new String(buf_iso88591, "gbk"); 

将编码转回 gbk。
 

我们本应该用gbk的编码来读取数据并解码成字符串,但结果却采用了ISO-8859-1的编码,导致生成一个错误的字符串。要恢复,就要先把字符串恢复成原始字节数组,然后通过正确的编码gbk再次解码成字 符串(即把以gbk编码的数据转成unicode的字符串)。注意,jvm中的字符串永远都是unicode编码的
但编码转换并不是负负得正那么简单,这里我们之所以可以正确地转换回来,是因为 ISO8859-1 是单字节编码,所以每个字节被按照原样转换为String ,也就是说,虽然这是一个错误的转换,但编码没有改变,所以我们仍然有机会把编码转换回来!


看完这两篇后,写了段测试代码: 

Java代码   收藏代码
  1. package com.fsti.enc;  
  2.   
  3. public class EncUtil {  
  4.       
  5.     public static String getHex(byte b)  
  6.     {  
  7.         String hex = Integer.toHexString(b & 0xff);  
  8.         if(hex.length()==1)  
  9.         {  
  10.             hex = "0" + hex;  
  11.         }  
  12.         return hex;  
  13.     }  
  14.   
  15.     public static void showBytes(byte[] buffer)  
  16.     {  
  17.         for(int i=0; i<buffer.length; i++)  
  18.         {  
  19.             System.out.print( getHex(buffer[i]) + " ");  
  20.         }  
  21.         System.out.println();  
  22.   
  23.     }  
  24.   
  25.     public static void showChar(String s)  
  26.     {  
  27.         for(int i=0; i<s.length(); i++)  
  28.         {  
  29.             System.out.print( getHex((byte)(s.charAt(i))) + " ");  
  30.         }  
  31.         System.out.println();  
  32.   
  33.     }  
  34.   
  35. }  


Java代码   收藏代码
  1. package com.fsti.enc;  
  2.   
  3.   
  4. /** 
  5.  * http://baike.baidu.com/view/2613676.htm 
  6.  * @author zch 
  7.  * @date Apr 20, 2012 
  8.  * 
  9.  */  
  10. public class Test_Iso8859_1 extends EncUtil{  
  11.     public static String s =   
  12.         "http://baike.baidu.com/view/2613676.htm\r\n" +  
  13.         "  ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF," +  
  14.         "0x00-0x7F之间完全和ASCII一致,0x80-0x9F之间是控制字符,0xA0-0xFF之间是文字符号。" +  
  15.         "ISO-8859-1收录的字符除ASCII收录的字符外,还包括西欧语言、希腊语、泰语、阿拉伯语、希伯来语" +  
  16.         "对应的文字符号。欧元符号出现的比较晚,没有被收录在ISO-8859-1当中。\r\n" +  
  17.         "  因为ISO-8859-1编码范围使用了单字节内的所有空间,在支持ISO-8859-1的系统中传输和存储其他任何" +  
  18.         "编码的字节流都不会被抛弃。换言之,把其他任何编码的字节流当作ISO-8859-1编码看待都没有问题。这是" +  
  19.         "个很重要的特性,MySQL数据库默认编码是Latin1就是利用了这个特性。ASCII编码是一个7位的容器," +  
  20.         "ISO-8859-1编码是一个8位的容器。\r\n" +  
  21.         "  Latin1是ISO-8859-1的别名,有些环境下写作Latin-1。";  
  22.   
  23.     public static void testChar(char c) throws Exception  
  24.     {  
  25.         //char c = '中';  
  26.         System.out.println( c );  
  27.         System.out.println( (int)c );  
  28.         System.out.println( Integer.toHexString(c) );  
  29.         if(c>255)  
  30.         {  
  31.             byte low = (byte)(c / 256);  
  32.             byte hight = (byte)(c % 256);  
  33.             System.out.println( getHex(low) );  
  34.             System.out.println( getHex(hight) );  
  35.         }  
  36.     }  
  37.       
  38.     public static void testString() throws Exception  
  39.     {  
  40.         for(int i=0; i<s.length(); i++)  
  41.         {  
  42.             testChar(s.charAt(i));  
  43.         }  
  44.     }  
  45.       
  46.   
  47.     public static void testString_02() throws Exception  
  48.     {  
  49.           
  50.         StringBuffer sb = new StringBuffer();  
  51.         for(int i=0; i<s.length(); i++)  
  52.         {  
  53.             char c = s.charAt(i);  
  54.             if(c>255)  
  55.             {  
  56.                 char hight = (char)(c / 256);  
  57.                 char low = (char)(c % 256);  
  58.                 sb.append(hight);  
  59.                 sb.append(low);  
  60.             }  
  61.             else  
  62.             {  
  63.                 sb.append(c);  
  64.             }  
  65.         }  
  66.         String sIso88591 = sb.toString();  
  67.         System.out.println(sIso88591);  
  68.           
  69.         byte[] buf = sIso88591.getBytes("iso-8859-1");  
  70.         showBytes(buf);  
  71.         String sUtf_8 = new String(buf, "gbk");  
  72.         System.out.println(sUtf_8);  
  73.     }  
  74.       
  75.     public static void testString_03() throws Exception  
  76.     {  
  77.         System.out.println("s:" + s);  
  78.           
  79.         byte[] buf_gbk = s.getBytes("gbk");  
  80.         System.out.println("buf_gbk:");  
  81.         showBytes(buf_gbk);  
  82.           
  83.         StringBuffer sb = new StringBuffer();  
  84.         for(int i=0; i<buf_gbk.length; i++)  
  85.         {  
  86.             char c = (char)buf_gbk[i];  
  87.             sb.append( c );  
  88.             System.out.println(c + ":" + getHex((byte)c));  
  89.         }  
  90.           
  91.         String sIso88591 = sb.toString();  
  92.         System.out.println("sIso88591:");  
  93.         System.out.println(sIso88591);  
  94.         System.out.println("showChar(sIso88591):");  
  95.         showChar(sIso88591);  
  96.           
  97.         byte[] buf_iso88591 = sIso88591.getBytes("iso-8859-1");  
  98.         System.out.println("buf_iso88591:");  
  99.         showBytes(buf_iso88591);  
  100.         String sUtf_8 = new String(buf_iso88591, "gbk");  
  101.         System.out.println(sUtf_8);  
  102.   
  103.     }  
  104.       
  105.     public static void testString_04() throws Exception  
  106.     {  
  107.         System.out.println("s:" + s);  
  108.           
  109.         byte[] buf_gbk = s.getBytes("gbk");  
  110.         System.out.println("buf_gbk:");  
  111.         showBytes(buf_gbk);  
  112.           
  113.         String sIso88591 = new String(buf_gbk, "iso-8859-1");  
  114.           
  115.         System.out.println("sIso88591:");  
  116.         System.out.println(sIso88591);  
  117.         System.out.println("showChar(sIso88591):");  
  118.         showChar(sIso88591);  
  119.           
  120.         byte[] buf_iso88591 = sIso88591.getBytes("iso-8859-1");  
  121.         System.out.println("buf_iso88591:");  
  122.         showBytes(buf_iso88591);  
  123.         String sGbk = new String(buf_iso88591, "gbk");  
  124.         System.out.println(sGbk);  
  125.   
  126.     }  
  127.       
  128.     /** 
  129.      * @param args 
  130.      */  
  131.     public static void main(String[] args) throws Exception{  
  132.         //testString();  
  133.         //testString_02();  
  134.         testString_04();  
  135.     }  
  136.   
  137. }  



参考资料: 
http://my.oschina.net/whp/blog/36846 
http://baike.baidu.com/view/2613676.htm

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics