教学之友,学习之友。

站长教学网

当前位置: 站长教学网 > 网站编程 > ASP教程 >

如何处理ASP页面UTF-8或GB2312编码乱码

时间:2012-03-29 22:06来源:未知 作者:ken 点击:

最好的方法:
先说一下基本的东西:
<%@ 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

'个人代码风格注释(变量名中第一个小写字母表表示变量类型)站长教学网 eduyo.com
'i:为Integer型;
's:为String;
Function U2UTF8(ByVal a_iNum)
Dim sResult
Dim sUTF8
Dim iTemp
Dim iHexNum
Dim 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
Dim sResult
Dim sTemp
Dim iLen
Dim iUnicode
Dim iTemp
Dim 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("中国人"))
 

(责任编辑:ken)

TAG标签: asp 乱码 编码 UTF-8 GB2312
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
注册登录:不允许匿名留言,登录后留言无需输入验证码。
栏目列表
最新内容