用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧VisualBasic

UTF-8转换GB2312函数(VB)

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 19:55:16

有网友要GBK转到UTF8的代码,暂时没有找到,提供一个UTF-8转换GB2312的,对照学习一下吧
'*/-------------------------------------------------------------
'*/模 块 名:mUTF8
'*/功    能:UTF-8转换GB2312函数
'*/示    例:UTF2GB("%E9%83%BD%E5%B8%82%E6%83%85%E7%B7%A3 %E6%98%9F%E5%BA%A7")
'*/-------------------------------------------------------------
Public Function UTF2GB(UTFStr As String) As String
    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

Public 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

Public Function c2to16(X)
    i = 1
    For i = 1 To Len(X) Step 4
        c2to16 = c2to16 & Hex(c2to10(Mid(X, i, 4)))
    Next
End Function

Public 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

Public 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

Public 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

Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论

相关文章

PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:5,265.62500 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号