---
title: "【转】java 与 iso-8859-1 编码"
author: "wangyanlong0107"
date: "2012-08-26T21:16:59Z"
tags: []
categories: ["编程语言"]
slug: "java-iso-8859-1"
draft: false
lastmod: "2012-12-17T16:30:50Z"
id: 1663281
url: "https://bigdragon.iteye.com/blog/1663281"
---

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

主要是研究 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代码      [![收藏代码](http://zwhc.iteye.com/images/icon_star.png)](http://zwhc.iteye.com/blog/1491202 "收藏这段代码")

01. package com.fsti.enc;
02. public class EncUtil {
03. public static String getHex(byte b)
04. {
05. String hex = Integer.toHexString(b & 0xff);
06. if(hex.length()==1)
07. {
08. hex = "0" + hex;
09. }
10. return hex;
11. }
12. public static void showBytes(byte\[] buffer)
13. {
14. for(int i=0; i&lt;buffer.length; i++)
15. {
16. System.out.print( getHex(buffer\[i]) + " ");
17. }
18. System.out.println();
19. }
20. public static void showChar(String s)
21. {
22. for(int i=0; i&lt;s.length(); i++)
23. {
24. System.out.print( getHex((byte)(s.charAt(i))) + " ");
25. }
26. System.out.println();
27. }
28. }

Java代码      [![收藏代码](http://zwhc.iteye.com/images/icon_star.png)](http://zwhc.iteye.com/blog/1491202 "收藏这段代码")

001. package com.fsti.enc;
002. /\**
003. * http://baike.baidu.com/view/2613676.htm
004. * @author zch
005. * @date Apr 20, 2012
006. \*
007. \*/
008. public class Test\_Iso8859\_1 extends EncUtil{
009. public static String s =
010. "http://baike.baidu.com/view/2613676.htm\\r\\n" +
011. "　　ISO-8859-1编码是单字节编码，向下兼容ASCII，其编码范围是0x00-0xFF，" +
012. "0x00-0x7F之间完全和ASCII一致，0x80-0x9F之间是控制字符，0xA0-0xFF之间是文字符号。" +
013. "ISO-8859-1收录的字符除ASCII收录的字符外，还包括西欧语言、希腊语、泰语、阿拉伯语、希伯来语" +
014. "对应的文字符号。欧元符号出现的比较晚，没有被收录在ISO-8859-1当中。\\r\\n" +
015. "　　因为ISO-8859-1编码范围使用了单字节内的所有空间，在支持ISO-8859-1的系统中传输和存储其他任何" +
016. "编码的字节流都不会被抛弃。换言之，把其他任何编码的字节流当作ISO-8859-1编码看待都没有问题。这是" +
017. "个很重要的特性，MySQL数据库默认编码是Latin1就是利用了这个特性。ASCII编码是一个7位的容器，" +
018. "ISO-8859-1编码是一个8位的容器。\\r\\n" +
019. "　　Latin1是ISO-8859-1的别名，有些环境下写作Latin-1。";
020. public static void testChar(char c) throws Exception
021. {
022. //char c = '中';
023. System.out.println( c );
024. System.out.println( (int)c );
025. System.out.println( Integer.toHexString(c) );
026. if(c&gt;255)
027. {
028. byte low = (byte)(c / 256);
029. byte hight = (byte)(c % 256);
030. System.out.println( getHex(low) );
031. System.out.println( getHex(hight) );
032. }
033. }
034. public static void testString() throws Exception
035. {
036. for(int i=0; i&lt;s.length(); i++)
037. {
038. testChar(s.charAt(i));
039. }
040. }
041. public static void testString\_02() throws Exception
042. {
043. StringBuffer sb = new StringBuffer();
044. for(int i=0; i&lt;s.length(); i++)
045. {
046. char c = s.charAt(i);
047. if(c&gt;255)
048. {
049. char hight = (char)(c / 256);
050. char low = (char)(c % 256);
051. sb.append(hight);
052. sb.append(low);
053. }
054. else
055. {
056. sb.append(c);
057. }
058. }
059. String sIso88591 = sb.toString();
060. System.out.println(sIso88591);
061. byte\[] buf = sIso88591.getBytes("iso-8859-1");
062. showBytes(buf);
063. String sUtf\_8 = new String(buf, "gbk");
064. System.out.println(sUtf\_8);
065. }
066. public static void testString\_03() throws Exception
067. {
068. System.out.println("s:" + s);
069. byte\[] buf\_gbk = s.getBytes("gbk");
070. System.out.println("buf\_gbk:");
071. showBytes(buf\_gbk);
072. StringBuffer sb = new StringBuffer();
073. for(int i=0; i&lt;buf\_gbk.length; i++)
074. {
075. char c = (char)buf\_gbk\[i];
076. sb.append( c );
077. System.out.println(c + ":" + getHex((byte)c));
078. }
079. String sIso88591 = sb.toString();
080. System.out.println("sIso88591:");
081. System.out.println(sIso88591);
082. System.out.println("showChar(sIso88591):");
083. showChar(sIso88591);
084. byte\[] buf\_iso88591 = sIso88591.getBytes("iso-8859-1");
085. System.out.println("buf\_iso88591:");
086. showBytes(buf\_iso88591);
087. String sUtf\_8 = new String(buf\_iso88591, "gbk");
088. System.out.println(sUtf\_8);
089. }
090. public static void testString\_04() throws Exception
091. {
092. System.out.println("s:" + s);
093. byte\[] buf\_gbk = s.getBytes("gbk");
094. System.out.println("buf\_gbk:");
095. showBytes(buf\_gbk);
096. String sIso88591 = new String(buf\_gbk, "iso-8859-1");
097. System.out.println("sIso88591:");
098. System.out.println(sIso88591);
099. System.out.println("showChar(sIso88591):");
100. showChar(sIso88591);
101. byte\[] buf\_iso88591 = sIso88591.getBytes("iso-8859-1");
102. System.out.println("buf\_iso88591:");
103. showBytes(buf\_iso88591);
104. String sGbk = new String(buf\_iso88591, "gbk");
105. System.out.println(sGbk);
106. }
107. /\**
108. * @param args
109. \*/
110. public static void main(String\[] args) throws Exception{
111. //testString();
112. //testString\_02();
113. testString\_04();
114. }
115. }

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