比如语言的“语”字,他的gb2312格式下二进制编码为D3 EF,而utf-8格式下编码为E8 AF AD,这个时候我需要做的是把”语“字的编码从D3 EF更换到E8 AF AD,并且以字符串显示出来,那个ado.stream应该是可以做到的,可是我有个特殊环境无法调用ado.stream,现在求用程序把gb2312格式的”语“转换为utf-8格式的“语”
...全文
69412打赏收藏
求asp程序,把汉字字符串转换为utf-8格式的字符串
比如语言的“语”字,他的gb2312格式下二进制编码为D3 EF,而utf-8格式下编码为E8 AF AD,这个时候我需要做的是把”语“字的编码从D3 EF更换到E8 AF AD,并且以字符串显示出来,那个ado.stream应该是可以做到的,可是我有个特殊环境无法调用ado.stream,现在求用程序把gb2312格式的”语“转换为utf-8格式的“语”
<script language="VBScript">
Public Function UTF8EncodeChar(z)
Dim c : c=AscW(z)'取UNICODE编码
if c>0 And c<256 Then'Asc编码直接返回
UTF8EncodeChar=z
Exit Function
End If
If c<0 Then c=c + &H10000&'VBScript的Integer溢出,加上
Dim k : k=CLng(c)'备份一个编码,后面判断要用
Dim b()
Dim i : i=0
While c>&H0&'将编码按照6位一组,分组存到字节数组 b 中
ReDim Preserve b(i)
b(i)=CByte(c And &H3F&)
c=c \ &H40&
i=i+1
Wend
If UBound(b)>0 Then '如果分开的6位组不止一个,除最高一组外,全部加上二进制10000000
For i=0 To UBound(b)-1
b(i)=b(i) + &H80
Next
End If
i=UBound(b)'根据字符的UNICODE编码范围,给最高组加上前缀
If k<=CLng(&H7F&) Then
b(i) = b(i) + 0
ElseIf k<=CLng(&H7FF&) Then
b(i) = b(i) + &HC0
ElseIf k<=Clng(&HFFFF&) Then
b(i) = b(i) + &HE0
ElseIf k<=CLng(&H1FFFFF&) Then
b(i) = b(i) + &HF0
ElseIf k<=CLng(&H3FFFFFF&) Then
b(i) = b(i) + &HF8
Else
b(i) = b(i) + &HFC
End If
UTF8EncodeChar=""
For i=UBound(b) To 0 Step -1'将分组转换成URL编码
UTF8EncodeChar=UTF8EncodeChar & "%" & Right("00" & Hex(b(i)),2)
Next
Erase b
End Function
Public Function UTF8EncodeString(s)
Dim i,l,c : l=Len(s)
For i=1 To l
UTF8EncodeString=UTF8EncodeString & UTF8EncodeChar(Mid(s,i,1))
Next
End Function
MsgBox UTF8EncodeString("OK")
</script>
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
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