7,762
社区成员
发帖
与我相关
我的任务
分享
'UTF-8 URL解码
Public Function UTF8_UrlDecode(ByVal URL As String)
On Error Resume Next
Dim B, ub ''中文字的Unicode码(2字节)
Dim UtfB ''Utf-8单个字节
Dim UtfB1, UtfB2, UtfB3 ''Utf-8码的三个字节
Dim i, n, s
n = 0
ub = 0
For i = 1 To Len(URL)
B = Mid(URL, i, 1)
Select Case B
Case "+"
s = s & " "
Case "%"
ub = Mid(URL, i + 1, 2)
UtfB = CInt("&H" & ub)
If UtfB < 128 Then
i = i + 2
s = s & ChrW(UtfB)
Else
UtfB1 = (UtfB And &HF) * &H1000 ''取第1个Utf-8字节的二进制后4位
UtfB2 = (CInt("&H" & Mid(URL, i + 4, 2)) And &H3F) * &H40 ''取第2个Utf-8字节的二进制后6位
UtfB3 = CInt("&H" & Mid(URL, i + 7, 2)) And &H3F ''取第3个Utf-8字节的二进制后6位
s = s & ChrW(UtfB1 Or UtfB2 Or UtfB3)
i = i + 8
End If
Case Else ''Ascii码
s = s & B
End Select
Next
UTF8_UrlDecode = s
End Function
book%3a%2f%2fssreader%2fe0%3furl%3dhttp%3a%2f%2f95%2e65%2e19%2e157%2f1013%2fdiskQNQ%2fQNQ40%2f10%2f%2100001%2epdg%26canread%3d1%26readreg%3d0%26candownload%3d1%26downloadreg%3d0%26bed%3d2015%2d03%2d12%26pagenum%3d%26pagetype%3d%26canprint%3d1%26printreg%3d0%26bookname%3d%u5e72%u90e8%u8981%u5e72%u6210%u4e8b%20%20%u505a%u65b0%u65f6%u671f%u6700%u4f18%u79c0%u7684%u5e72%u90e8%26pages%3d213%26rd%3d6463616767616563616565643137313030353233%26dm%3d2%26sdwen%3d1%26a%3d731499A5B3F6F13E4D8DBA855F8257AD%26from%3dhttp%3a%2f%2fcxbook%2enlic%2enet%2ecn%3a8089%2fmarkbook%2fGoBook%2ejsp%3fBID%3d13209957%26ReadMode%3d0%26displaystyle%3d
book://ssreader/e0?url=http://95.65.19.157/1013/diskQNQ/QNQ40/10/!00001.pdg&canread=1&readreg=0&candownload=1&downloadreg=0&bed=2015-03-12&pagenum=&pagetype=&canprint=1&printreg=0&bookname=干部要干成事 做新时期最优秀的干部&pages=213&rd=6463616767616563616565643137313030353233&dm=2&sdwen=1&a=731499A5B3F6F13E4D8DBA855F8257AD&from=http://cxbook.nlic.net.cn:8089/markbook/GoBook.jsp?BID=13209957&ReadMode=0&displaystyle=
book://ssreader/e0?url=http://95.65.19.157/1013/diskQNQ/QNQ40/10/!00001.pdg&canread=1&readreg=0&candownload=1&downloadreg=0&bed=2015-03-12&pagenum=&pagetype=&canprint=1&printreg=0&bookname==e72=0e8=981=e72=210=e8b 05a 5b0 5f6 71f 700 f18 9c0 684 e72 0e8&pages=213&rd=6463616767616563616565643137313030353233&dm=2&sdwen=1&a=731499A5B3F6F13E4D8DBA855F8257AD&from=http://cxbook.nlic.net.cn:8089/markbook/GoBook.jsp?BID=13209957&ReadMode=0&displaystyle=
'GBK URL解码
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 Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
'常用的代码页:
const cpUTF8 =65001
const cpGB2312 = 936
const cpGB18030=54936
const cpUTF7 =65000
Function MultiByteToUTF16(UTF8() As Byte, CodePage As Long) As String
Dim bufSize As Long
bufSize = MultiByteToWideChar(CodePage, 0&, UTF8(0), UBound(UTF8) + 1, 0, 0)
MultiByteToUTF16 = Space(bufSize)
MultiByteToWideChar CodePage, 0&, UTF8(0), UBound(UTF8) + 1, StrPtr(MultiByteToUTF16), bufSize
End Function
Function UTF16ToMultiByte(UTF16 As String, CodePage As Long) As Byte()
Dim bufSize As Long
Dim arr() As Byte
bufSize = WideCharToMultiByte(CodePage, 0&, StrPtr(UTF16), Len(UTF16), 0, 0, 0, 0)
ReDim arr(bufSize - 1)
WideCharToMultiByte CodePage, 0&, StrPtr(UTF16), Len(UTF16), arr(0), bufSize, 0, 0
UTF16ToMultiByte = arr
End Function
Private Sub Command1_Click()
MsgBox MultiByteToUTF16(UTF16ToMultiByte("ab中,c", cpUTF8), cpUTF8)
End Sub