VB如何把汉字和字母转换成utf8字符?
答案:3 悬赏:10 手机版
解决时间 2021-04-03 09:43
- 提问者网友:听门外雪花风
- 2021-04-02 19:13
VB如何把汉字和字母转换成utf8字符?
最佳答案
- 五星知识达人网友:狂恋
- 2021-04-02 20:50
参考方法如下:
面的两段VB代码分别针对UTF-8(UTF8EncodeURI)和GB2312(GBKEncodeURI)进行了编码的转换。
Private Sub command1_click()
Debug.Print (UTF8EncodeURI("汉字"))
Debug.Print (GBKEncodeURI("汉字"))
End Sub
Function UTF8EncodeURI(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
UTF8EncodeURI = szRet
End Function
Function GBKEncodeURI(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
GBKEncodeURI = szRet
End Function
面的两段VB代码分别针对UTF-8(UTF8EncodeURI)和GB2312(GBKEncodeURI)进行了编码的转换。
Private Sub command1_click()
Debug.Print (UTF8EncodeURI("汉字"))
Debug.Print (GBKEncodeURI("汉字"))
End Sub
Function UTF8EncodeURI(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
UTF8EncodeURI = szRet
End Function
Function GBKEncodeURI(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
GBKEncodeURI = szRet
End Function
全部回答
- 1楼网友:骨子里都是戏
- 2021-04-02 21:25
汉字和字母转UTF8字符?
应该不是这吧?是不是你弄错了啊~追问我怎么会弄错- -。追答uft8是一种字符集编码,而非像你所说的
我想你所说的应该是汉字和字母转化为ascii码吧~追问拿比如说石柱的编码是%E7%9F%B3%E6%9F%B1
那漫的编码怎么得出~追答
应该不是这吧?是不是你弄错了啊~追问我怎么会弄错- -。追答uft8是一种字符集编码,而非像你所说的
我想你所说的应该是汉字和字母转化为ascii码吧~追问拿比如说石柱的编码是%E7%9F%B3%E6%9F%B1
那漫的编码怎么得出~追答