对于使用UTF-8的Google搜索引擎来说,使用Google搜索“汉字”会变成http://www.google.com/search?q= %E6%B1%89%E5%AD%97 ,而对于使用GB2312的百度搜索引擎来说,使用百度搜索“汉字”会变成另外的http://www.baidu.com/s?wd=%BA%BA%D7%D6 。下面的两段VB代码分别针对UTF-8(UTF8EncodeURI)和GB2312(GBKEncodeURI)进行了编码的转换。
Private Sub command1_click()
Debug.Print (UTF8EncodeURL("汉字"))
Debug.Print (GBKEncodeURL("汉字"))
End Sub
Function UTF8EncodeURL(szInput)
Dim wch, uch, szRet
Dim x
Dim nAsc, nAsc2, nAsc3
If szInput = "" Then
UTF8EncodeURI = szInput
Exit Function
End If
For x = 1 To Len(szInput)
wch = Mid(szInput, x, 1)
nAsc = AscW(wch)
If nAsc < 0 Then nAsc = nAsc + 65536
If (nAsc And &HFF80) = 0 Then
szRet = szRet & wch
Else
If (nAsc And &HF000) = 0 Then
uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
szRet = szRet & uch
Else
uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _
Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _
Hex(nAsc And &H3F Or &H80)
szRet = szRet & uch
End If
End If
Next
UTF8EncodeURL= szRet
End Function
Function GBKEncodeURL(szInput)
Dim i As Long
Dim x() As Byte
Dim szRet As String
szRet = ""
x = StrConv(szInput, vbFromUnicode)
For i = LBound(x) To UBound(x)
szRet = szRet & "%" & Hex(x(i))
Next
GBKEncodeURL = szRet
End Function
[本日志由 田草 于 2008-05-11 02:22 PM 编辑]
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |