当前位置:编程学习 > VB >>

VB 搜索 字符转化

在使用搜索引擎的时候,输入的内容就转化成诸如“%E5%B2%B8%E8%BE%B9&btnG=%E6%90%9C%E7%B4%A2%E5%9B%BE%E7%89”之类
在VB中,怎样实现此功能?
追问:我想做成Google、Baidu之类的效果,可程序运行生成的“%D1%A7%D0%A3”直接输入地址栏时却查询的是乱码~~
答案:
Public Function URLEncode(ByRef strURL As String) As String
Dim I As Long
Dim tempStr As String
For I = 1 To Len(strURL)
If Asc(Mid(strURL, I, 1)) < 0 Then
tempStr = "%" & Right(CStr(Hex(Asc(Mid(strURL, I, 1)))), 2)
tempStr = "%" & Left(CStr(Hex(Asc(Mid(strURL, I, 1)))), Len(CStr(Hex(Asc(Mid(strURL, I, 1))))) - 2) & tempStr
URLEncode = URLEncode & tempStr
ElseIf (Asc(Mid(strURL, I, 1)) >= 65 And Asc(Mid(strURL, I, 1)) <= 90) Or (Asc(Mid(strURL, I, 1)) >= 97 And Asc(Mid(strURL, I, 1)) <= 122) Then
URLEncode = URLEncode & Mid(strURL, I, 1)
Else
URLEncode = URLEncode & "%" & Hex(Asc(Mid(strURL, I, 1)))
End If
Next
End Function

Public Function URLDecode(ByRef strURL As String) As String
Dim I As Long

If InStr(strURL, "%") = 0 Then URLDecode = strURL: Exit Function

For I = 1 To Len(strURL)
If Mid(strURL, I, 1) = "%" Then
If Val("&H" & Mid(strURL, I + 1, 2)) > 127 Then
URLDecode = URLDecode & Chr(Val("&H" & Mid(strURL, I + 1, 2) & Mid(strURL, I + 4, 2)))
I = I + 5
Else
URLDecode = URLDecode & Chr(Val("&H" & Mid(strURL, I + 1, 2)))
I = I + 2
End If
Else
URLDecode = URLDecode & Mid(strURL, I, 1)
End If
Next
End Function

Private Sub Command1_Click()

Dim Old_URL As String
Dim New_URL As String
Old_URL = URLEncode("测试")
New_URL = URLDecode(Old_URL)
Debug.Print Old_URL
Debug.Print New_URL

End Sub

上一个:vb连sql
下一个:vb与sql问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,