'*/-------------------------------------------------------------
'*/模 块 名: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