如何用EXCEL VBA将汉字转成GB2312的URL编码,如输入"中国",生成"%D6%D0%B9%FA",用于网络查询.
答案:2 悬赏:40 手机版
解决时间 2021-04-03 11:56
- 提问者网友:别再叽里呱啦
- 2021-04-03 05:19
如何用EXCEL VBA将汉字转成GB2312的URL编码,如输入"中国",生成"%D6%D0%B9%FA",用于网络查询.
最佳答案
- 五星知识达人网友:長槍戰八方
- 2021-04-03 06:27
据说有 MultiByteToWideChar 和 WideCharToMultiByte 这两个函数可以使用,但是我没用过!
写了2个函数,自己改吧!
Public Function uncode(x As Range) As String
On Error Resume Next
MsgBox Asc(Mid(x, 1, 1)) + 65536
If x.Columns.Count <> 1 Then
uncode = "Error"
Exit Function
End If
If Len(x) = 0 Then Exit Function
Dim a, b, c, i
b = ""
For i = 1 To Len(x)
a = Mid(x, i, 1)
If Asc(a) < 0 Then
uncode = uncode & sixteen(Asc(a) + 65536)
Else
uncode = uncode & a
End If
Next
End Function
Public Function sixteen(m As Long) As String
n = m
x = "": y = ""
Do While n <> 0
a = n Mod 2
n = n \ 2
x = a & x
Loop
Do While Len(x) Mod 4 <> 0
x = "0" + x
Loop
Do While Len(x) > 0
Select Case Right(x, 4)
Case "0000"
y = "0" + y
Case "0001"
y = "1" + y
Case "0010"
y = "2" + y
Case "0011"
y = "3" + y
Case "0100"
y = "4" + y
Case "0101"
y = "5" + y
Case "0110"
y = "6" + y
Case "0111"
y = "7" + y
Case "1000"
y = "8" + y
Case "1001"
y = "9" + y
Case "1010"
y = "A" + y
Case "1011"
y = "B" + y
Case "1100"
y = "C" + y
Case "1101"
y = "D" + y
Case "1110"
y = "E" + y
Case "1111"
y = "F" + y
End Select
x = Left(x, Len(x) - 4)
Loop
sixteen = y
End Function来自:求助得到的回答
写了2个函数,自己改吧!
Public Function uncode(x As Range) As String
On Error Resume Next
MsgBox Asc(Mid(x, 1, 1)) + 65536
If x.Columns.Count <> 1 Then
uncode = "Error"
Exit Function
End If
If Len(x) = 0 Then Exit Function
Dim a, b, c, i
b = ""
For i = 1 To Len(x)
a = Mid(x, i, 1)
If Asc(a) < 0 Then
uncode = uncode & sixteen(Asc(a) + 65536)
Else
uncode = uncode & a
End If
Next
End Function
Public Function sixteen(m As Long) As String
n = m
x = "": y = ""
Do While n <> 0
a = n Mod 2
n = n \ 2
x = a & x
Loop
Do While Len(x) Mod 4 <> 0
x = "0" + x
Loop
Do While Len(x) > 0
Select Case Right(x, 4)
Case "0000"
y = "0" + y
Case "0001"
y = "1" + y
Case "0010"
y = "2" + y
Case "0011"
y = "3" + y
Case "0100"
y = "4" + y
Case "0101"
y = "5" + y
Case "0110"
y = "6" + y
Case "0111"
y = "7" + y
Case "1000"
y = "8" + y
Case "1001"
y = "9" + y
Case "1010"
y = "A" + y
Case "1011"
y = "B" + y
Case "1100"
y = "C" + y
Case "1101"
y = "D" + y
Case "1110"
y = "E" + y
Case "1111"
y = "F" + y
End Select
x = Left(x, Len(x) - 4)
Loop
sixteen = y
End Function来自:求助得到的回答
全部回答
- 1楼网友:枭雄戏美人
- 2021-04-03 07:27
我在C#程序中会转,EXCEL中你可以用函数,VBA中不知道用什么,你用CODE(‘中’)把中字转成数字码54992,然后再用DEC2HEx(54992,4)转成十六进制数D6D0,VBA的方法我再看看。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯