• java字符编码方式总结


    java字符编码方式总结

    一、概要
      在JAVA应用程序特别是基于WEB的程序中,经常遇到字符的编码问题。为了防止出现乱码,首先需要了解JAVA是如何处理字符的,这样就可以有目的地在输入/输出环节中增加必要的转码。其次,由于各种服务器有不同的处理方式,还需要多做试验,确保使用中不出现乱码。
    二、基本概念
    2.1 JAVA中字符的表达
      JAVA中有char、byte、String这几个概念。char 指的是一个UNICODE字符,为16位的整数。byte 是字节,字符串在网络传输或存储前需要转换为byte数组。在从网络接收或从存储设备读取后需要将byte数组转换成String。String是字符串,可以看成是由char组成的数组。String 和 char 为内存形式,byte是网络传输或存储的序列化形式。
    举例:

    String ying = “英”;
    char ying = ying.charAt(0);
    String yingHex = Integer.toHexString(ying);
    82 F1
    byte yingGBBytes = ying.getBytes(“GBK”);
    GB编码的字节数值
    D3 A2
    2.2 编码方式的简介
      String序列化成byte数组或反序列化时需要选择正确的编码方式。如果编码方式不正确,就会得到一些0x3F的值。常用的字符编码方式有ISO8859_1、GB2312、GBK、UTF-8/UTF-16/UTF-32。
    ISO8859_1用来编码拉丁文,它由单字节(0-255)组成。
      GB2312、GBK用来编码简体中文,它有单字节和双字节混合组成。最高位为1的字节和下一个字节构成一个汉字,最高位为0的字节是ASCII码。
      UTF-8/UTF-16/UTF-32是国际标准UNICODE的编码方式。 用得最多的是UTF-8,主要是因为它在对拉丁文编码时节约空间。
    UNICODE值 UTF-8编码
    U-00000000 - U-0000007F: 0xxxxxxx
    U-00000080 - U-000007FF: 110xxxxx 10xxxxxx
    U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
    U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
    U-00200000 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
    U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
    三、J2SE中相关的函数
    String str =”英”;
    //取得GB2312编码的字节
    byte[] bytesGB2312 = str.getBytes(“GB2312”);
    //取得平台缺省编码的字节(solaris为ISO8859_1,windows为GB2312)
    byte[] bytesDefault = str.getBytes();
    //用指定的编码将字节转换成字符串
    String newStrGB = new String(bytesGB2312, “GB2312”);

    //用平台缺省的编码将字节转换成字符串(solaris为ISO8859_1,windows为GB2312)
    String newStrDefault = new String(bytesDefault);
    //用指定的编码从字节流里面读取字符
    InputStream in = xxx;
    InputStreamReader reader = InputStreamReader( in, “GB2312”);
    char aChar = reader.read();
    四、JSP、数据库的编码
    4.1 JSP中的编码
    (1) 静态声明:
    CHARSET有两个作用:
    JSP文件的编码方式:在读取JSP文件、生成JAVA类时,源JSP文件中汉字的编码
    JSP输出流的编码方式:在执行JSP时,往response流里面写入数据的编码方式
    (2) 动态改变:在往response流里面写数据前可以调用response.setContentType(),设定正确的编码类型。
    (3) 在TOMCAT中,由Request.getParameter() 得到的参数,编码方式都是ISO8859_1。所以如果在浏览器输入框内输入一个汉字“英”,在服务器端就得到一个ISO8859_1编码的(0x00,0xD3,0x00,0xA2)。所以通常在接收参数时转码:
    String wrongStr = response.getParameter(“name”);
    String correctStr = new String(wrongStr.getBytes(“ISO8859_1”),”GB2312”);
    在最新的SERVLET规范里面,也可以在获取参数之前执行如下代码:
    request.setCharacterEncoding(“GB2312”);
    4.2 数据库的编码
    (1) 数据库使用UTF-16
    如果String中是UNICODE字符,写入读出时不需要转码
    (2) 数据库使用ISO8859_1
    如果String中是UNICODE字符,写入读出时需要转码
    写入:String newStr = new String(oldStr.getByte(“GB2312”), “ISO8859_1”);
    读出:String newStr = new String(oldStr.getByte(“ISO8859_1”),”GB2312”);
    五、源文件的编码
    5.1 资源文件
    资源文件的编码方式和编辑平台相关。在WINDOWS平台下编写的资源文件,以GB2312方式编码。在编译时需要转码,以确保在各个平台上的正确性:
    native2ascii –encoding GB2312 source.properties
    这样从资源文件中读出的就是正确的UNICODE字符串。
    5.2 源文件
    源文件的编码方式和编辑平台相关。在WINDOWS平台下开发的源文件,以GB2312方式编码。在编译的时候,需要指定源文件的编码方式:
    javac –encoding GB2312
    JAVA编译后生成的字节文件的编码为UTF-8。


    ①最新版TOMCAT4.1.18支持request.setCharacterEncoding(String enc)
    ②资源文件转码成company.name=\u82f1\u65af\u514b
    ③如果数据库使用utf-16则不需要这部分转码
    ④页面上应有
    转码ⅰ:
    String s = new String
    (request.getParameter(“name”).getBytes(“ISO8859_1”),”GB2312”);
    转码ⅱ:
    String s = new String(name.getBytes(“GB2312”),”ISO8859_1”);
    转码ⅲ:
    String s = new String(name.getBytes(“ISO8859_1”),” GB2312”);
    一、关键技术点:
    1、当前流行的字符编码格式有:US-ASCII、ISO-8859-1、UTF-8、UTF-16BE、UTF-16LE、UTF-16、GBK、GB2312等,其中GBK、GB2312是专门处理中文编码的。
    2、String的getBytes方法用于按指定编码获取字符串的字节数组,参数指定了解码格式,如果没有指定解码格式,则按系统默认编码格式。
    3、String的“String(bytes[] bs, String charset)”构造方法用于把字节数组按指定的格式组合成一个字符串对象


    二、实例演示:


    package book.String;
    import java.io.UnsupportedEncodingException;
    /** *//**
    * 转换字符串的编码
    * @author joe
    *
    */
    public class ChangeCharset ...{
    /** *//** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块 */
    public static final String US_ASCII = "US-ASCII";
    /** *//** ISO拉丁字母表 No.1,也叫做ISO-LATIN-1 */
    public static final String ISO_8859_1 = "ISO-8859-1";
    /** *//** 8 位 UCS 转换格式 */
    public static final String UTF_8 = "UTF-8";
    /** *//** 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序 */
    public static final String UTF_16BE = "UTF-16BE";
    /** *//** 16 位 UCS 转换格式,Litter Endian(最高地址存放地位字节)字节顺序 */
    public static final String UTF_16LE = "UTF-16LE";
    /** *//** 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识 */
    public static final String UTF_16 = "UTF-16";
    /** *//** 中文超大字符集 **/
    public static final String GBK = "GBK";

    public static final String GB2312 = "GB2312";

    /** *//** 将字符编码转换成US-ASCII码 */
    public String toASCII(String str) throws UnsupportedEncodingException ...{
    return this.changeCharset(str, US_ASCII);
    }

    /** *//** 将字符编码转换成ISO-8859-1 */
    public String toISO_8859_1(String str) throws UnsupportedEncodingException ...{
    return this.changeCharset(str, ISO_8859_1);
    }

    /** *//** 将字符编码转换成UTF-8 */
    public String toUTF_8(String str) throws UnsupportedEncodingException ...{
    return this.changeCharset(str, UTF_8);
    }

    /** *//** 将字符编码转换成UTF-16BE */
    public String toUTF_16BE(String str) throws UnsupportedEncodingException...{
    return this.changeCharset(str, UTF_16BE);
    }

    /** *//** 将字符编码转换成UTF-16LE */
    public String toUTF_16LE(String str) throws UnsupportedEncodingException ...{
    return this.changeCharset(str, UTF_16LE);
    }

    /** *//** 将字符编码转换成UTF-16 */
    public String toUTF_16(String str) throws UnsupportedEncodingException ...{
    return this.changeCharset(str, UTF_16);
    }

    /** *//** 将字符编码转换成GBK */
    public String toGBK(String str) throws UnsupportedEncodingException ...{
    return this.changeCharset(str, GBK);
    }

    /** *//** 将字符编码转换成GB2312 */
    public String toGB2312(String str) throws UnsupportedEncodingException ...{
    return this.changeCharset(str,GB2312);
    }

    /** *//**
    * 字符串编码转换的实现方法
    * @param str 待转换的字符串
    * @param newCharset 目标编码
    */
    public String changeCharset(String str, String newCharset) throws UnsupportedEncodingException ...{
    if(str != null) ...{
    //用默认字符编码解码字符串。与系统相关,中文windows默认为GB2312
    byte[] bs = str.getBytes();
    return new String(bs, newCharset); //用新的字符编码生成字符串
    }
    return null;
    }

    /** *//**
    * 字符串编码转换的实现方法
    * @param str 待转换的字符串
    * @param oldCharset 源字符集
    * @param newCharset 目标字符集
    */
    public String changeCharset(String str, String oldCharset, String newCharset) throws UnsupportedEncodingException ...{
    if(str != null) ...{
    //用源字符编码解码字符串
    byte[] bs = str.getBytes(oldCharset);
    return new String(bs, newCharset);
    }
    return null;
    }

    public static void main(String[] args) throws UnsupportedEncodingException ...{
    ChangeCharset test = new ChangeCharset();
    String str = "This is a 中文的 String!";
    System.out.println("str:" + str);

    String gbk = test.toGBK(str);
    System.out.println("转换成GBK码:" + gbk);
    System.out.println();

    String ascii = test.toASCII(str);
    System.out.println("转换成US-ASCII:" + ascii);
    System.out.println();

    String iso88591 = test.toISO_8859_1(str);
    System.out.println("转换成ISO-8859-1码:" + iso88591);
    System.out.println();

    gbk = test.changeCharset(iso88591, ISO_8859_1, GBK);
    System.out.println("再把ISO-8859-1码的字符串转换成GBK码:" + gbk);
    System.out.println();

    String utf8 = test.toUTF_8(str);
    System.out.println();
    System.out.println("转换成UTF-8码:" + utf8);
    String utf16be = test.toUTF_16BE(str);
    System.out.println("转换成UTF-16BE码:" + utf16be);
    gbk = test.changeCharset(utf16be, UTF_16BE, GBK);
    System.out.println("再把UTF-16BE编码的字符转换成GBK码:" + gbk);
    System.out.println();

    String utf16le = test.toUTF_16LE(str);
    System.out.println("转换成UTF-16LE码:" + utf16le);
    gbk = test.changeCharset(utf16le, UTF_16LE, GBK);
    System.out.println("再把UTF-16LE编码的字符串转换成GBK码:" + gbk);
    System.out.println();

    String utf16 = test.toUTF_16(str);
    System.out.println("转换成UTF-16码:" + utf16);
    String gb2312 = test.changeCharset(utf16, UTF_16, GB2312);
    System.out.println("再把UTF-16编码的字符串转换成GB2312码:" + gb2312);
    }
    }


    文章出处:http://www.diybl.com/course/3_program/java/javaxl/20071126/87571.html


  • 相关阅读:
    JQuery操作Javascript对象和数组的工具函数总览
    .document.execCommand("BackgroundImageCache",false,true)解决ie6下的背景图片缓存问题
    jQuery boxy弹出层对话框插件中文演示及讲解
    纯客户端页面关键字搜索高亮jQuery插件
    Android游戏开发中地图图层开发
    第一次开通博客
    C#下的Config类, 支持Get, Set, GetList
    输出N以内素数的方法
    使用 googleperftools 剖析程序性能瓶颈
    在网页设计中寻找热情
  • 原文地址:https://www.cnblogs.com/zdcaolei/p/2122937.html
Copyright © 2020-2023  润新知