来源:
http://hi.baidu.com/zhenyk/blog/item/f5158f5108d3cc898d543067.html
string utfinfo = "document.write(/"alert('aa你好么??');/");";
string gb2312info = string.Empty;
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding("gb2312");
// Convert the string into a byte[].
byte[] unicodeBytes = utf8.GetBytes(utfinfo);
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);
// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
gb2312.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
gb2312info = new string(asciiChars);
汉字转换为UTF-8
function chinese2unicode(Str)
dim i
dim Str_one
dim Str_unicode
for i=1 to len(Str)
Str_one=Mid(Str,i,1)
Str_unicode=Str_unicode&chr(38)
Str_unicode=Str_unicode&chr(35)
Str_unicode=Str_unicode&chr(120)
Str_unicode=Str_unicode& Hex(ascw(Str_one))
Str_unicode=Str_unicode&chr(59)
next
Response.Write Str_unicode
end function
UTF-8 To GB2312
function UTF2GB(UTFStr)
for Dig=1 to len(UTFStr)
if mid(UTFStr,Dig,1)="%" then
if len(UTFStr) >= Dig+8 then
GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
Dig=Dig+8
else
GBStr=GBStr & mid(UTFStr,Dig,1)
end if
else
GBStr=GBStr & mid(UTFStr,Dig,1)
end if
next
UTF2GB=GBStr
end function
function ConvChinese(x)
A=split(mid(x,2),"%")
i=0
j=0
for i=0 to ubound(A)
A(i)=c16to2(A(i))
next
for i=0 to ubound(A)-1
DigS=instr(A(i),"0")
Unicode=""
for j=1 to DigS-1
if j=1 then
A(i)=right(A(i),len(A(i))-DigS)
Unicode=Unicode & A(i)
else
i=i+1
A(i)=right(A(i),len(A(i))-2)
Unicode=Unicode & A(i)
end if
next
if len(c2to16(Unicode))=4 then
ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))
else
ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))
end if
next
end function
function c2to16(x)
i=1
for i=1 to len(x) step 4
c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
next
end function
function c2to10(x)
c2to10=0
if x="0" then exit function
i=0
for i= 0 to len(x) -1
if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
next
end function
function c16to2(x)
i=0
for i=1 to len(trim(x))
tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
do while len(tempstr)<4
tempstr="0" & tempstr
loop
c16to2=c16to2 & tempstr
next
end function
function c10to2(x)
mysign=sgn(x)
x=abs(x)
DigS=1
do
if x<2^DigS then
exit do
else
DigS=DigS+1
end if
loop
tempnum=x
i=0
for i=DigS to 1 step-1
if tempnum>=2^(i-1) then
tempnum=tempnum-2^(i-1)
c10to2=c10to2 & "1"
else
c10to2=c10to2 & "0"
end if
next
if mysign=-1 then c10to2="-" & c10to2
end function
1、'UTF转GB---将UTF8编码文字转换为GB编码文字
function UTF2GB(UTFStr)
for Dig=1 to len(UTFStr)
'如果UTF8编码文字以%开头则进行转换
if mid(UTFStr,Dig,1)="%" then
'UTF8编码文字大于8则转换为汉字
if len(UTFStr) >= Dig+8 then
GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
Dig=Dig+8
else
GBStr=GBStr & mid(UTFStr,Dig,1)
end if
else
GBStr=GBStr & mid(UTFStr,Dig,1)
end if
next
UTF2GB=GBStr
end function
'UTF8编码文字将转换为汉字
function ConvChinese(x)
A=split(mid(x,2),"%")
i=0
j=0
for i=0 to ubound(A)
A(i)=c16to2(A(i))
next
for i=0 to ubound(A)-1
DigS=instr(A(i),"0")
Unicode=""
for j=1 to DigS-1
if j=1 then
A(i)=right(A(i),len(A(i))-DigS)
Unicode=Unicode & A(i)
else
i=i+1
A(i)=right(A(i),len(A(i))-2)
Unicode=Unicode & A(i)
end if
next
if len(c2to16(Unicode))=4 then
ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))
else
ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))
end if
next
end function
问题的症状是偶数字数的中文可以正常传递,奇数字数的中文最后一个字会被砍掉。
Google了一下,解决的办法主要有以下几种:
1.设置web.config文件。
......
culture="zh-CN" fileEncoding="gb2312" />
......
2.采用模板列
从实际应用来看,第一种方法更简单一些,基本不需要修改多少代码,直接加上就可以了。但是
会引发其他的一些问题。第二种需要对代码稍作修改,副作用小。
在使用ajax.net的UpdatePanel的时候,当requestEncoding编码为GB2312的时候,出现乱码。如
果要解决这个问题最简单的就是改用utf-8了,但是原来使用GB2312,现在不能随便改成utf-8的
怎么办呢?
我原来是想这样做的:
byte[] buffer = System.Text.Encoding.Default.GetBytes
(tbxRemark.Text);
string remark = System.Text.Encoding.UTF8.GetString(buffer);
tbxRemark.Text = remark;
order.Remark = remark;
但是结果最后一个字总是乱码,没办法只好是这样做了:
string s = System.Text.Encoding.UTF8.GetString(Request.BinaryRead
(Request.ContentLength));
NameValueCollection form = HttpUtility.ParseQueryString(s);
tbxRemark.Text = form[tbxRemark.UniqueID];
order.Remark = tbxRemark.Text;
这里写出来,希望有点儿用!
研究好多天了,也试过好多办法了,总结出目前发现最好的方法:
先说一下基本的东西:
<%@ codepage=936%>简体中文
<%@ codepage=950%>繁体中文
<%@ codepage=65001%>UTF-8
codepage指定了IIS按什么编码读取传递过来的串串(表单提交,地址栏传递等)。
出乱码的原因也就是网站要整合的时候模块编码不一样引起的。
就像我的博客一样,整合的时候都会出这个问题,因为BLOG是Utf-8的,
近来很多网友都在为这个问题咨询,我尝试了很多种方法。
最方便的方法如下:
不要转换任何模块网页的编码该utf-8的还是utf-8,该Gb22312的还是Gb2312
在Utf-8模块的包文件(如conn.asp,但是要注意conn.asp必须是在第一行调用)最前面加上
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Session.CodePage=65001%>
在GB2312模块的包文件最前面加上
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Session.CodePage=936%>
其他编码的类推。再出问题,我也帮你了了,我现在是都没问题了^^
url传递中文的解决方案总结 新客网 XKER.COM 2006-05-10 收藏本文添加到百度搜藏 收藏到
QQ书签 添加到雅虎收藏+
baidu
1.设置web.config文件。(我不喜欢设置成这样)
......
fileEncoding="gb2312" />
......
或者:
aspx文件中:
2.传递中文之前,将要传递的中文参数进行编码,在接收时再进行解码。
>> 进行传递
string Name = "中文参数";
Response.Redirect("B.aspx?Name="+Server.UrlEncode(Name));
>> 进行接收
string Name = Request.QueryString["Name"];
Response.Write(Server.UrlDecode(Name));
或者:
NavigateURL='<%# "WebForm2.aspx?Singer=" + HttpUtility.UrlEncode("中国人",
System.Text.Encoding.GetEncoding("GB2312")) %>'
3.如果是从 .HTML 文件向 .Aspx 文件进行传递中文参数的话(即不从后台用 Redirect()方法
进行 Url 转换)。一样要将传递的中文参数进行编码,在接收时再进行解码。
>> 进行传递
>> 进行接收
string Name = Request.QueryString["Name"];
Response.Write(Server.UrlDecode(Name));
一般来说。设置web.config文件就可以了。但是如果你用 JavaScript 调用 webservice 方法的
话(往webservice里面传递中文参数)。设置 web.config 文件好象无效。
————————————————————
在html中实现编解码:
在新窗口保存
20040603123628交易中心网上集中交易系统合同
当前位置保存,无闪烁。
易系统合同.doc")>20040603123628交易中心网上集中交易系统合同
注意:路径中的斜线是:“/”,而不是“/”,否则也不行啊。
----------------------------------------
前一阵遇到在做.net Web开发时,碰到一个很奇怪的问题,就是Url中,如果将中文字符作为参
数值传递时,QueryString得到的值可能会出错。简单的说,比如下面这个Url:
UrlParmTest.aspx?parm1=中国&parm2=中国人
在Request.QueryString时,parm1和parm2得到都是"中国",显然出现了问题,可是在某些情况
下却是正常的。
如果请求不是直接通过URL,而使用Response.Redirect在服务器端操作,没有遇到过类似的问题
。
当时我想中文是双字节编码,可能传递的时候就是有不确定性,还是用英文好。
可是为什么在Server端Redirect就是正常的,问题在哪里呢?
:
如果在.cs文件中设置中文参数,请在中文参数外使用Server.UrlEncode("中文")对中文进行
Encode
如果在.aspx文件中设置,请使用<%=Server.UrlEncode("中文")%>进行Encode
在QueryString时,不用再进行Decode,可以获得正常的中文字符串
下面是给出的一些解释:
UrlEncode把一些多字节字符转换成url里允许的单字节字符,本来浏览器就会自动做的,但是目
前确实存在一些问题,所以自己再Encode一下,在接受端会自动对Url进行Decode。
我想Response.Redirect可能可以确保作Encode的工作,所以没有问题。