当前位置: 移动技术网 > IT编程>开发语言>Asp > ASP UTF-8页面乱码+GB2312转UTF-8 +生成UTF-8格式的文件(编码)第1/2页

ASP UTF-8页面乱码+GB2312转UTF-8 +生成UTF-8格式的文件(编码)第1/2页

2017年12月12日  | 移动技术网IT编程  | 我要评论
最好的方法:
先说一下基本的东西:
<%@ codepage=65001%>utf-8
<%@ codepage=936%>简体中文
<%@ codepage=950%>繁体中文
<%@ codepage=437 %>美国/加拿大英语
<%@ codepage=932 %>日文
<%@ codepage=949 %>韩文
<%@ codepage=866 %>俄文

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%>
其他编码的类推。
asp中汉字与utf-8的互相转换
'=============汉字转换为utf-8==================

function chinese2unicode(str)
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
chinese2unicode = str_unicode
end function



'=============utf-8转换为汉字==================

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

gb2312转utf-8

'个人代码风格注释(变量名中第一个小写字母表表示变量类型)
'i:为integer型;
's:为string;
function u2utf8(byval a_inum)
dim sresult,sutf8
dim itemp,ihexnum,i

ihexnum = trim(a_inum)

if ihexnum = "" then
exit function
end if

sresult = ""

if (ihexnum < 128) then
sresult = sresult & ihexnum
elseif (ihexnum < 2048) then
sresult = chrb(&h80 + (ihexnum and &h3f))
ihexnum = ihexnum \ &h40
sresult = chrb(&hc0 + (ihexnum and &h1f)) & sresult
elseif (ihexnum < 65536) then
sresult = chrb(&h80 + (ihexnum and &h3f))
ihexnum = ihexnum \ &h40
sresult = chrb(&h80 + (ihexnum and &h3f)) & sresult
ihexnum = ihexnum \ &h40
sresult = chrb(&he0 + (ihexnum and &hf)) & sresult
end if

u2utf8 = sresult
end function

function gb2utf(byval a_sstr)
dim sgb,sresult,stemp
dim ilen,iunicode,itemp,i

sgb = trim(a_sstr)
ilen = len(sgb)
for i = 1 to ilen
stemp = mid(sgb,i,1)
itemp = asc(stemp)

if (itemp>127 or itemp<0) then
iunicode = ascw(stemp)
if iunicode<0 then
iunicode = iunicode + 65536
end if
else
iunicode = itemp
end if

sresult = sresult & u2utf8(iunicode)
next

gb2utf = sresult
end function

'调用方法
response.binarywrite(gb2utf("中国人"))
1

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网