地址栏的要接收的gbk数据 n=%d9%f8%d5%fe 怎么把 接收到的gbk数据 转为 utf-8 数据 能存到数据库里,现在存进去的是????

解决方案 »

  1.   

    可以用System.Text.Encoding中提供的方法,下面是一个例子:
            static void Main(string[] args)
            {
                String str = "测试数据";
                Encoding gb2312 = Encoding.GetEncoding("GB2312");
                Byte[] gb2312Bytes = gb2312.GetBytes(str);            // Test starts: GB2312 bytes are what you have now
                Console.WriteLine("============= GB2312 bytes ================");
                foreach (Byte b in gb2312Bytes)
                {
                    Console.Write("{0:x} ", b);
                }
                Console.WriteLine();
                Console.WriteLine("===========================================");            String str1 = gb2312.GetString(gb2312Bytes);
                //Console.WriteLine(str1);
                Byte[] utf8Bytes = Encoding.UTF8.GetBytes(str1);            // Test result: UTF-8 bytes are what you want
                Console.WriteLine("============= UTF-8 bytes =================");
                foreach (Byte b in utf8Bytes)
                {
                    Console.Write("{0:x} ", b);
                }
                Console.WriteLine();
                Console.WriteLine("===========================================");
                Console.ReadLine();
            }程序输出结果:
    ============= GB2312 bytes ================
    b2 e2 ca d4 ca fd be dd
    ===========================================
    ============= UTF-8 bytes =================
    e6 b5 8b e8 af 95 e6 95 b0 e6 8d ae
    ===========================================
      

  2.   

    在web.config里面,配置
    <globalization requestEncoding="gb2312" responseEncoding="gb2312"/>,不需任何解密转换,即可
        String str = Request.QueryString["n"]; 
        Response.Write(str);
      

  3.   

    我这里写的是客户端调用的API,发过来的是GBK数据,要转换成UTF-8的,不是GB2312我知道php的写法是
    mb_convert_encoding(str, "UTF-8", "GBK"); asp.net该怎么写?
      

  4.   

    你先贴下接受到数据。如果是你要发送 QueryString 的 Request还需要用 HttpUtility.UrlEncode("xxxx", System.Text.Encoding.UTF8);