在www.baidu.com里搜索 “你好”后url对汉字的编码为:wd=%C4%E3%BA%C3,而在google中搜索"你好"后url对汉字的编码为:q=%E4%BD%A0%E5%A5%BD,
很明显,这两家搜索引擎对汉字的编码方式是不一样的,我想知道他们分别是用了什么编码方式,java里对应的是哪个api? 还是他们都是自己实现的非标准的编码方式?谢谢。

解决方案 »

  1.   

    public static void main(String[] args)throws Exception
        {
            String s = "你好";
            byte[] utf8 = s.getBytes("UTF8");
            System.out.println("UTF8: " + binaryToString(utf8));
            byte[] gb2312 = s.getBytes("GB2312");
            System.out.println("GB2312: " + binaryToString(gb2312));
            byte[] gbk = s.getBytes("GBK");
            System.out.println("GBK: " + binaryToString(gbk));
        }
        private static String binaryToString(byte[] b)
        {
            StringBuffer sb = new StringBuffer();
            for(int i=0;i<b.length;i++)
            {
                int v = b[i] & 0xff;
                if (i > 0)
                    sb.append(" ");
                sb.append(Integer.toHexString(v/16));
                sb.append(Integer.toHexString(v%16));
            }
            return sb.toString();
        }
    }
    输出结果:
    UTF8: e4 bd a0 e5 a5 bd
    GB2312: c4 e3 ba c3
    GBK: c4 e3 ba c3由此可见google是UTF8编码,这种方式是针对UNICODE的通用编码方式,可支持所有UNICODE字符集。
    baidu是GB2312(或GBK),这种方式只针对中文字符集,这是由于baidu只做中文搜索。在这两个主页的html源文件里(查看源文件就可以看到)
    google的
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    baidu的
    <meta http-equiv=Content-Type content="text/html; charset=gb2312">
    浏览器就是根据这个来进行编码的。