会员登陆
登录加载中...
超级搜索
栏  目  
类  别  
关键词  
 站内搜索   网络搜索
  
 当前位置:文章首页>>网页编程>>ASP实例>>GB2312转UTF-8
推荐给你的朋友阅读:

GB2312转UTF-8

来源:蓝色理想 发表日期: 2006-06-07 19:27:55
    

    网页编辑转换。

    个人代码风格注释(变量名中第一个小写字母表表示变量类型)
    '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("中国人"))

     ======雨城注:上述代码有点小错(如果需要转换的字符串都是汉字就没问题。但是如果遇到中间有数字或字母的时候就会被截断,而得不到正确的结果。),下面是正确代码======

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 & ChrB(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.Write(GB2UTF("冥蝶"))’可以得到正确结果
Response.Write(GB2UTF("冥蝶308641836"))’得到:冥蝶308641836,结果不再被截断!

0
0
[作者:不详 关键词:UTF-8 录入:raincity 责编:raincity 阅读次数:8389]

相关文章 相关新闻 相关软件 相关教程 公共评论 发表评论

  • ·暂无相关内容
  • ·暂无相关内容
    评论载入中...
    您的称呼  验 证 码   
    您的评论 [ 最多字数:  已用字数:剩余字数:]

     
COPYRIGHT 2003-2010 创意飞腾科技有限公司(The Feitec LTD.) ALL RIGHTS RESERVED .
官方咨询官方咨询4376161 官方咨询526743959 技术支持:FeitecCMS5366464
页面加载时间:108.89ms Powered By:FeitecCMS V4.0(Build1101)
[蜀ICP备05000881]