java 字符编码方式

来源:互联网 发布:淘宝买虚拟产品 编辑:程序博客网 时间:2024/06/05 04:04
  1. java字符编码方式总结  
  2.   
  3. 一、概要  
  4.   在JAVA应用程序特别是基于WEB的程序中,经常遇到字符的编码问题。为了防止出现乱码,首先需要了解JAVA是如何处理字符的,这样就可以有目的地在输入/输出环节中增加必要的转码。其次,由于各种服务器有不同的处理方式,还需要多做试验,确保使用中不出现乱码。  
  5. 二、基本概念  
  6. 21 JAVA中字符的表达  
  7.   JAVA中有charbyte、String这几个概念。char 指的是一个UNICODE字符,为16位的整数。byte 是字节,字符串在网络传输或存储前需要转换为byte数组。在从网络接收或从存储设备读取后需要将byte数组转换成String。String是字符串,可以看成是由char组成的数组。String 和 char 为内存形式,byte是网络传输或存储的序列化形式。  
  8. 举例:  
  9. 英  
  10. String ying = “英”;  
  11. char ying = ying.charAt(0);  
  12. String yingHex = Integer.toHexString(ying);  
  13. 82 F1   
  14. byte yingGBBytes = ying.getBytes(“GBK”);  
  15. GB编码的字节数值  
  16. D3 A2   
  17. 22 编码方式的简介  
  18.   String序列化成byte数组或反序列化时需要选择正确的编码方式。如果编码方式不正确,就会得到一些0x3F的值。常用的字符编码方式有ISO8859_1、GB2312、GBK、UTF-8/UTF-16/UTF-32。  
  19. ISO8859_1用来编码拉丁文,它由单字节(0255)组成。  
  20.   GB2312、GBK用来编码简体中文,它有单字节和双字节混合组成。最高位为1的字节和下一个字节构成一个汉字,最高位为0的字节是ASCII码。  
  21.   UTF-8/UTF-16/UTF-32是国际标准UNICODE的编码方式。 用得最多的是UTF-8,主要是因为它在对拉丁文编码时节约空间。  
  22. UNICODE值 UTF-8编码  
  23. U-00000000 - U-0000007F: 0xxxxxxx  
  24. U-00000080 - U-000007FF: 110xxxxx 10xxxxxx   
  25. U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx   
  26. U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx   
  27. U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx   
  28. U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx   
  29. 三、J2SE中相关的函数  
  30. String str =”英”;  
  31. //取得GB2312编码的字节  
  32. byte[] bytesGB2312 = str.getBytes(“GB2312”);   
  33. //取得平台缺省编码的字节(solaris为ISO8859_1,windows为GB2312)  
  34. byte[] bytesDefault = str.getBytes();  
  35. //用指定的编码将字节转换成字符串  
  36. String newStrGB = new String(bytesGB2312, “GB2312”);  
  37.   
  38. //用平台缺省的编码将字节转换成字符串(solaris为ISO8859_1,windows为GB2312)  
  39. String newStrDefault = new String(bytesDefault);  
  40. //用指定的编码从字节流里面读取字符  
  41. InputStream in = xxx;  
  42. InputStreamReader reader = InputStreamReader( in, “GB2312”);  
  43. char aChar = reader.read();  
  44. 四、JSP、数据库的编码  
  45. 41 JSP中的编码  
  46. (1) 静态声明:  
  47. CHARSET有两个作用:  
  48. JSP文件的编码方式:在读取JSP文件、生成JAVA类时,源JSP文件中汉字的编码  
  49. JSP输出流的编码方式:在执行JSP时,往response流里面写入数据的编码方式  
  50. (2) 动态改变:在往response流里面写数据前可以调用response.setContentType(),设定正确的编码类型。  
  51. (3) 在TOMCAT中,由Request.getParameter() 得到的参数,编码方式都是ISO8859_1。所以如果在浏览器输入框内输入一个汉字“英”,在服务器端就得到一个ISO8859_1编码的(0x00,0xD3,0x00,0xA2)。所以通常在接收参数时转码:  
  52. String wrongStr = response.getParameter(“name”);  
  53. String correctStr = new String(wrongStr.getBytes(“ISO8859_1”),”GB2312”);  
  54. 在最新的SERVLET规范里面,也可以在获取参数之前执行如下代码:  
  55. request.setCharacterEncoding(“GB2312”);  
  56. 42 数据库的编码  
  57. (1) 数据库使用UTF-16  
  58. 如果String中是UNICODE字符,写入读出时不需要转码  
  59. (2) 数据库使用ISO8859_1  
  60. 如果String中是UNICODE字符,写入读出时需要转码  
  61. 写入:String newStr = new String(oldStr.getByte(“GB2312”), “ISO8859_1”);  
  62. 读出:String newStr = new String(oldStr.getByte(“ISO8859_1”),”GB2312”);  
  63. 五、源文件的编码  
  64. 51 资源文件  
  65. 资源文件的编码方式和编辑平台相关。在WINDOWS平台下编写的资源文件,以GB2312方式编码。在编译时需要转码,以确保在各个平台上的正确性:  
  66. native2ascii –encoding GB2312 source.properties  
  67. 这样从资源文件中读出的就是正确的UNICODE字符串。  
  68. 52 源文件  
  69. 源文件的编码方式和编辑平台相关。在WINDOWS平台下开发的源文件,以GB2312方式编码。在编译的时候,需要指定源文件的编码方式:  
  70. javac –encoding GB2312  
  71. JAVA编译后生成的字节文件的编码为UTF-8。  
  72.   
  73.   
  74. ①最新版TOMCAT4.1.18支持request.setCharacterEncoding(String enc)  
  75. ②资源文件转码成company.name=\u82f1\u65af\u514b  
  76. ③如果数据库使用utf-16则不需要这部分转码  
  77. ④页面上应有  
  78. 转码ⅰ:  
  79. String s = new String  
  80. (request.getParameter(“name”).getBytes(“ISO8859_1”),”GB2312”);  
  81. 转码ⅱ:  
  82. String s = new String(name.getBytes(“GB2312”),”ISO8859_1”);  
  83. 转码ⅲ:  
  84. String s = new String(name.getBytes(“ISO8859_1”),” GB2312”);  
  85. 一、关键技术点:  
  86. 1、当前流行的字符编码格式有:US-ASCII、ISO-8859-1、UTF-8、UTF-16BE、UTF-16LE、UTF-16、GBK、GB2312等,其中GBK、GB2312是专门处理中文编码的。  
  87. 2、String的getBytes方法用于按指定编码获取字符串的字节数组,参数指定了解码格式,如果没有指定解码格式,则按系统默认编码格式。  
  88. 3、String的“String(bytes[] bs, String charset)”构造方法用于把字节数组按指定的格式组合成一个字符串对象  
  89.   
  90.   
  91. 二、实例演示:   
  92.   
  93.   
  94. package book.String;  
  95. import java.io.UnsupportedEncodingException;  
  96. /** *//** 
  97. * 转换字符串的编码 
  98. * @author joe 
  99. * 
  100. */  
  101. public class ChangeCharset ...{  
  102. /** *//** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块    */  
  103. public static final String US_ASCII = "US-ASCII";  
  104. /** *//** ISO拉丁字母表 No.1,也叫做ISO-LATIN-1     */  
  105. public static final String ISO_8859_1 = "ISO-8859-1";  
  106. /** *//** 8 位 UCS 转换格式     */  
  107. public static final String UTF_8 = "UTF-8";  
  108. /** *//** 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序     */  
  109. public static final String UTF_16BE = "UTF-16BE";  
  110. /** *//** 16 位 UCS 转换格式,Litter Endian(最高地址存放地位字节)字节顺序     */  
  111. public static final String UTF_16LE = "UTF-16LE";  
  112. /** *//** 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识     */  
  113. public static final String UTF_16 = "UTF-16";  
  114. /** *//** 中文超大字符集     **/  
  115. public static final String GBK = "GBK";  
  116.   
  117. public static final String GB2312 = "GB2312";  
  118.   
  119. /** *//** 将字符编码转换成US-ASCII码     */  
  120. public String toASCII(String str) throws UnsupportedEncodingException ...{  
  121.        return this.changeCharset(str, US_ASCII);  
  122. }  
  123.   
  124. /** *//** 将字符编码转换成ISO-8859-1     */  
  125. public String toISO_8859_1(String str) throws UnsupportedEncodingException ...{  
  126.        return this.changeCharset(str, ISO_8859_1);  
  127. }  
  128.   
  129. /** *//** 将字符编码转换成UTF-8     */  
  130. public String toUTF_8(String str) throws UnsupportedEncodingException ...{  
  131.        return this.changeCharset(str, UTF_8);  
  132. }  
  133.   
  134. /** *//** 将字符编码转换成UTF-16BE     */  
  135. public String toUTF_16BE(String str) throws UnsupportedEncodingException...{  
  136.        return this.changeCharset(str, UTF_16BE);  
  137. }  
  138.   
  139. /** *//** 将字符编码转换成UTF-16LE     */  
  140. public String toUTF_16LE(String str) throws UnsupportedEncodingException ...{  
  141.        return this.changeCharset(str, UTF_16LE);  
  142. }  
  143.   
  144. /** *//** 将字符编码转换成UTF-16     */  
  145. public String toUTF_16(String str) throws UnsupportedEncodingException ...{  
  146.        return this.changeCharset(str, UTF_16);  
  147. }  
  148.   
  149. /** *//** 将字符编码转换成GBK     */  
  150. public String toGBK(String str) throws UnsupportedEncodingException ...{  
  151.        return this.changeCharset(str, GBK);  
  152. }  
  153.   
  154. /** *//** 将字符编码转换成GB2312     */  
  155. public String toGB2312(String str) throws UnsupportedEncodingException ...{  
  156.        return this.changeCharset(str,GB2312);  
  157. }  
  158.   
  159. /** *//** 
  160.     * 字符串编码转换的实现方法 
  161.     * @param str 待转换的字符串 
  162.     * @param newCharset 目标编码 
  163.     */  
  164. public String changeCharset(String str, String newCharset) throws UnsupportedEncodingException ...{  
  165.        if(str != null) ...{  
  166.          //用默认字符编码解码字符串。与系统相关,中文windows默认为GB2312  
  167.          byte[] bs = str.getBytes();  
  168.          return new String(bs, newCharset); //用新的字符编码生成字符串  
  169.        }  
  170.        return null;  
  171. }  
  172.   
  173. /** *//** 
  174.     * 字符串编码转换的实现方法 
  175.     * @param str 待转换的字符串 
  176.     * @param oldCharset 源字符集 
  177.     * @param newCharset 目标字符集 
  178.     */  
  179. public String changeCharset(String str, String oldCharset, String newCharset) throws UnsupportedEncodingException ...{  
  180.        if(str != null) ...{  
  181.          //用源字符编码解码字符串  
  182.          byte[] bs = str.getBytes(oldCharset);  
  183.          return new String(bs, newCharset);  
  184.        }  
  185.        return null;  
  186. }  
  187.   
  188. public static void main(String[] args) throws UnsupportedEncodingException ...{  
  189.        ChangeCharset test = new ChangeCharset();  
  190.        String str = "This is a 中文的 String!";  
  191.        System.out.println("str:" + str);  
  192.         
  193.        String gbk = test.toGBK(str);  
  194.        System.out.println("转换成GBK码:" + gbk);  
  195.        System.out.println();  
  196.         
  197.        String ascii = test.toASCII(str);  
  198.        System.out.println("转换成US-ASCII:" + ascii);  
  199.        System.out.println();  
  200.         
  201.        String iso88591 = test.toISO_8859_1(str);  
  202.        System.out.println("转换成ISO-8859-1码:" + iso88591);  
  203.        System.out.println();  
  204.         
  205.        gbk = test.changeCharset(iso88591, ISO_8859_1, GBK);  
  206.        System.out.println("再把ISO-8859-1码的字符串转换成GBK码:" + gbk);  
  207.        System.out.println();  
  208.         
  209.        String utf8 = test.toUTF_8(str);  
  210.        System.out.println();  
  211.        System.out.println("转换成UTF-8码:" + utf8);  
  212.        String utf16be = test.toUTF_16BE(str);  
  213.        System.out.println("转换成UTF-16BE码:" + utf16be);  
  214.        gbk = test.changeCharset(utf16be, UTF_16BE, GBK);  
  215.        System.out.println("再把UTF-16BE编码的字符转换成GBK码:" + gbk);  
  216.        System.out.println();  
  217.         
  218.        String utf16le = test.toUTF_16LE(str);  
  219.        System.out.println("转换成UTF-16LE码:" + utf16le);  
  220.        gbk = test.changeCharset(utf16le, UTF_16LE, GBK);  
  221.        System.out.println("再把UTF-16LE编码的字符串转换成GBK码:" + gbk);  
  222.        System.out.println();  
  223.         
  224.        String utf16 = test.toUTF_16(str);  
  225.        System.out.println("转换成UTF-16码:" + utf16);  
  226.        String gb2312 = test.changeCharset(utf16, UTF_16, GB2312);  
  227.        System.out.println("再把UTF-16编码的字符串转换成GB2312码:" + gb2312);  
  228. }  
  229. }   
  230. 文章出处:http://www.diybl.com/course/3_program/java/javaxl/20071126/87571.html
0 0
原创粉丝点击