VB 如何将字符串变量中的乱码字符转换为可识别可读的字符

在程序执行过程中,某字符串的变量实际值为如下类似的看不懂的文字,怎么将这些乱码看不懂的文字转换为可识别的简体中文字呢?

骞惰拏鑺变箣姊︽崲镟
鍙ょ瓭

'UTF-8(UTF8EncodeURI)和GB2312(GBKEncodeURI)编码的转换2007年11月16日 星期五 19:50  在使用Google或者百度进行中文搜索的时候,我们会发现搜索过程中URL地址栏会变成一串格式字符串编码,我们编写程序经常也需要进行中文汉字到这类URL编码的转换,这里介绍两段相关的转换函数。
'
'  对于使用UTF-8的Google搜索引擎来说,使用Google搜索“汉字”会变成http://www.google.com/search?q=%E6%B1%89%E5%AD%97 ,而对于使用GB2312的百度搜索引擎来说,使用百度搜索“汉字”会变成另外的 http://www.baidu.com/s?wd=%BA%BA%D7%D6 。下面的两段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个回答  2011-09-22
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= 48 And KeyAscii <= 57 Then
Text2 = Text2 & Chr(KeyAscii)
ElseIf KeyAscii >= 65 And KeyAscii <= 90 Then
KeyAscii = KeyAscii + 32
Text2 = Text2 & Chr(KeyAscii)
KeyAscii = KeyAscii - 32
ElseIf KeyAscii >= 97 And KeyAscii <= 122 Then
KeyAscii = KeyAscii - 32
Text2 = Text2 & Chr(KeyAscii)
KeyAscii = KeyAscii + 32
Else
MsgBox "输入错误"
End If
End Sub
第2个回答  2011-09-21
因误会
相似回答