Public Function urlDecode(s As String) As String Dim i As Long Dim j As Long Dim tmp As String Dim c As String Dim L As Long Dim b() As Byte On Error GoTo errhandle If Len(s) = 0 Then Exit Function L = Len(s) ReDim b(L - 1) j = 0 For i = 1 To L c = Mid(s, i, 1) If c = "+" Then b(j) = Asc(" "): j = j + 1 ElseIf c = "%" Then b(j) = Val("&H" + Mid(s, i + 1, 2)): j = j + 1 i = i + 2 Else b(j) = Asc(c): j = j + 1 End If Next i ReDim Preserve b(j - 1) tmp = StrConv(b, vbUnicode) urlDecode = tmp Exit Function errhandle: logtofile "urlDecode(" + s + ") 出现 #" & str(Err.Number) & " 意外错误: " & Err.Description urlDecode = "_" Exit Function End Function Public Function urlEncode(s As String) As String Dim tmp As String Dim c As String Dim i As Long Dim L As Long Dim b() As Byte On Error GoTo errhandle If Len(s) = 0 Then Exit Function b = StrConv(s, vbFromUnicode) L = UBound(b) For i = 0 To L c = Chr(b(i)) Select Case c Case "A" To "Z", "a" To "z", "0" To "9", "-", ".", "/", ":", "=", "?", "~" ',"&" tmp = tmp + c Case Else tmp = tmp + "%" + Right("0" + Hex(b(i)), 2) If b(i) > 128 Then i = i + 1 tmp = tmp + "%" + Right("0" + Hex(b(i)), 2) End If End Select Next i urlEncode = tmp Exit Function errhandle: logtofile "urlEncode(" + s + ") 出现 #" & str(Err.Number) & " 意外错误: " & Err.Description urlEncode = "_" Exit Function End Function
看看里面的 UnicodeToGB2312 函数可能对你有用: http://blog.csdn.net/SupermanKing/archive/2010/11/05/5989227.aspx '进行 URL 编码 Public Function URLEncode(bstrIn As String) As String Dim TempStr As String, X As Long, TextAscII As String, QM As Long, WM As Long, ReturnStr As String ReturnStr = "" If Len(bstrIn) = 0 Then URLEncode = "": Exit Function For X = 1 To Len(bstrIn) TempStr = Mid(bstrIn, X, 1) TextAscII = Asc(TempStr) If Int(TextAscII) < 0 Then TextAscII = 65536 + Int(TextAscII) QM = TextAscII Mod 256 WM = Int(TextAscII / 256) Mod 256 ReturnStr = ReturnStr & "%" & String(2 - Len(Hex(WM)), "0") & Hex(WM) & "%" & String(2 - Len(Hex(QM)), "0") & Hex(QM) Else If (Int(TextAscII) >= Asc("A") And Int(TextAscII) <= Asc("Z")) Or (Int(TextAscII) >= Asc("a") And Int(TextAscII) <= Asc("z")) Or (Int(TextAscII) >= Asc("0") And Int(TextAscII) <= Asc("9")) Or Int(TextAscII) = Asc("-") Or Int(TextAscII) = Asc("+") Then ReturnStr = ReturnStr & TempStr Else ReturnStr = ReturnStr & "%" & String(2 - Len(Hex(TextAscII)), "0") & Hex(TextAscII) End If End If Next X URLEncode = ReturnStr End FunctionPublic Function UnURLEncode(bstrIn As String) As String Dim TempString As String Dim TempChar As String Dim StrArray() As Byte Dim CharCount As Long If Len(bstrIn) = 0 Then UnURLEncode = "": Exit Function
TempString = Replace(bstrIn, "+", " ", , , vbTextCompare) CharCount = 0 ReDim StrArray(CharCount) For X = 1 To Len(TempString) ReDim Preserve StrArray(CharCount) TempChar = Mid(TempString, X, 1) If TempChar = "%" Then TempChar = Mid(TempString, X + 1, 2) StrArray(CharCount) = Int("&H" & TempChar) X = X + 2 Else StrArray(CharCount) = Asc(TempChar) End If CharCount = CharCount + 1 Next X UnURLEncode = StrConv(StrArray, vbUnicode) End Function
6楼错的 Private Sub Form_Load() s = "'Telewins (HK) Co., Ltd." s_right = "%27Telewins+%28HK%29+Co.%2C+Ltd." MsgBox URLEncode(s & "") = s_right End Sub Public Function URLEncode(bstrIn As String) As String Dim TempStr As String, X As Long, TextAscII As String, QM As Long, WM As Long, ReturnStr As String ReturnStr = "" If Len(bstrIn) = 0 Then URLEncode = "": Exit Function For X = 1 To Len(bstrIn) TempStr = Mid(bstrIn, X, 1) TextAscII = Asc(TempStr) If Int(TextAscII) < 0 Then TextAscII = 65536 + Int(TextAscII) QM = TextAscII Mod 256 WM = Int(TextAscII / 256) Mod 256 ReturnStr = ReturnStr & "%" & String(2 - Len(Hex(WM)), "0") & Hex(WM) & "%" & String(2 - Len(Hex(QM)), "0") & Hex(QM) Else If (Int(TextAscII) >= Asc("A") And Int(TextAscII) <= Asc("Z")) Or (Int(TextAscII) >= Asc("a") And Int(TextAscII) <= Asc("z")) Or (Int(TextAscII) >= Asc("0") And Int(TextAscII) <= Asc("9")) Or Int(TextAscII) = Asc("-") Or Int(TextAscII) = Asc("+") Then ReturnStr = ReturnStr & TempStr Else ReturnStr = ReturnStr & "%" & String(2 - Len(Hex(TextAscII)), "0") & Hex(TextAscII) End If End If Next X URLEncode = ReturnStr End Function
Dim i As Long
Dim j As Long
Dim tmp As String
Dim c As String
Dim L As Long
Dim b() As Byte
On Error GoTo errhandle
If Len(s) = 0 Then Exit Function
L = Len(s)
ReDim b(L - 1)
j = 0
For i = 1 To L
c = Mid(s, i, 1)
If c = "+" Then
b(j) = Asc(" "): j = j + 1
ElseIf c = "%" Then
b(j) = Val("&H" + Mid(s, i + 1, 2)): j = j + 1
i = i + 2
Else
b(j) = Asc(c): j = j + 1
End If
Next i
ReDim Preserve b(j - 1)
tmp = StrConv(b, vbUnicode)
urlDecode = tmp
Exit Function
errhandle:
logtofile "urlDecode(" + s + ") 出现 #" & str(Err.Number) & " 意外错误: " & Err.Description
urlDecode = "_"
Exit Function
End Function
Public Function urlEncode(s As String) As String
Dim tmp As String
Dim c As String
Dim i As Long
Dim L As Long
Dim b() As Byte
On Error GoTo errhandle
If Len(s) = 0 Then Exit Function
b = StrConv(s, vbFromUnicode)
L = UBound(b)
For i = 0 To L
c = Chr(b(i))
Select Case c
Case "A" To "Z", "a" To "z", "0" To "9", "-", ".", "/", ":", "=", "?", "~" ',"&"
tmp = tmp + c
Case Else
tmp = tmp + "%" + Right("0" + Hex(b(i)), 2)
If b(i) > 128 Then
i = i + 1
tmp = tmp + "%" + Right("0" + Hex(b(i)), 2)
End If
End Select
Next i
urlEncode = tmp
Exit Function
errhandle:
logtofile "urlEncode(" + s + ") 出现 #" & str(Err.Number) & " 意外错误: " & Err.Description
urlEncode = "_"
Exit Function
End Function
http://blog.csdn.net/SupermanKing/archive/2010/11/05/5989227.aspx
'进行 URL 编码
Public Function URLEncode(bstrIn As String) As String
Dim TempStr As String, X As Long, TextAscII As String, QM As Long, WM As Long, ReturnStr As String
ReturnStr = ""
If Len(bstrIn) = 0 Then URLEncode = "": Exit Function
For X = 1 To Len(bstrIn)
TempStr = Mid(bstrIn, X, 1)
TextAscII = Asc(TempStr)
If Int(TextAscII) < 0 Then
TextAscII = 65536 + Int(TextAscII)
QM = TextAscII Mod 256
WM = Int(TextAscII / 256) Mod 256
ReturnStr = ReturnStr & "%" & String(2 - Len(Hex(WM)), "0") & Hex(WM) & "%" & String(2 - Len(Hex(QM)), "0") & Hex(QM)
Else
If (Int(TextAscII) >= Asc("A") And Int(TextAscII) <= Asc("Z")) Or (Int(TextAscII) >= Asc("a") And Int(TextAscII) <= Asc("z")) Or (Int(TextAscII) >= Asc("0") And Int(TextAscII) <= Asc("9")) Or Int(TextAscII) = Asc("-") Or Int(TextAscII) = Asc("+") Then
ReturnStr = ReturnStr & TempStr
Else
ReturnStr = ReturnStr & "%" & String(2 - Len(Hex(TextAscII)), "0") & Hex(TextAscII)
End If
End If
Next X
URLEncode = ReturnStr
End FunctionPublic Function UnURLEncode(bstrIn As String) As String
Dim TempString As String
Dim TempChar As String
Dim StrArray() As Byte
Dim CharCount As Long
If Len(bstrIn) = 0 Then UnURLEncode = "": Exit Function
TempString = Replace(bstrIn, "+", " ", , , vbTextCompare)
CharCount = 0
ReDim StrArray(CharCount)
For X = 1 To Len(TempString)
ReDim Preserve StrArray(CharCount)
TempChar = Mid(TempString, X, 1)
If TempChar = "%" Then
TempChar = Mid(TempString, X + 1, 2)
StrArray(CharCount) = Int("&H" & TempChar)
X = X + 2
Else
StrArray(CharCount) = Asc(TempChar)
End If
CharCount = CharCount + 1
Next X
UnURLEncode = StrConv(StrArray, vbUnicode)
End Function
Private Sub Form_Load()
s = "'Telewins (HK) Co., Ltd."
s_right = "%27Telewins+%28HK%29+Co.%2C+Ltd."
MsgBox URLEncode(s & "") = s_right
End Sub
Public Function URLEncode(bstrIn As String) As String
Dim TempStr As String, X As Long, TextAscII As String, QM As Long, WM As Long, ReturnStr As String
ReturnStr = ""
If Len(bstrIn) = 0 Then URLEncode = "": Exit Function
For X = 1 To Len(bstrIn)
TempStr = Mid(bstrIn, X, 1)
TextAscII = Asc(TempStr)
If Int(TextAscII) < 0 Then
TextAscII = 65536 + Int(TextAscII)
QM = TextAscII Mod 256
WM = Int(TextAscII / 256) Mod 256
ReturnStr = ReturnStr & "%" & String(2 - Len(Hex(WM)), "0") & Hex(WM) & "%" & String(2 - Len(Hex(QM)), "0") & Hex(QM)
Else
If (Int(TextAscII) >= Asc("A") And Int(TextAscII) <= Asc("Z")) Or (Int(TextAscII) >= Asc("a") And Int(TextAscII) <= Asc("z")) Or (Int(TextAscII) >= Asc("0") And Int(TextAscII) <= Asc("9")) Or Int(TextAscII) = Asc("-") Or Int(TextAscII) = Asc("+") Then
ReturnStr = ReturnStr & TempStr
Else
ReturnStr = ReturnStr & "%" & String(2 - Len(Hex(TextAscII)), "0") & Hex(TextAscII)
End If
End If
Next X
URLEncode = ReturnStr
End Function