Public Function HexToStr(ByVal strs As String) As String
Dim i As Integer, tmp As String, n
If Len(strs) Mod 2 Then Exit Function
For i = 1 To Len(strs) Step 2
n = Val("&H" & Mid(strs, i, 2))
If n < 0 Or n > 127 Then
n = Val("&H" & Mid(strs, i, 4))
i = i + 2
End If
tmp = tmp & Chr(n)
Next i
HexToStr = tmp
End FunctionPrivate Sub Command1_Click()
Text2.Text = HexToStr(Text1.Text)
End SubPrivate Sub Form_Load()
Text1.Text = "E3809053696D656EE38091E78E89E69E973C3C3C2A41
2E6C7379E68898E9989FE8BF9EE8B7B3E69C8DE58AA1E599A8"
End Sub
如上代码,只能转出乱码
“銆怱imen銆戠帀鏋?<<*A.lsy鎴橀槦杩炶烦链嶅姟鍣”有人说这个字符串要这么转“它的编码是UTF8的Unicode字符,这个
想还原字符就有些麻烦了,首先从16进制转为UTF8格式,然后转为
数值,最后转ASCII,还原后的字符串为结果”具体操作怎么做?谢谢。希望把转出来的结果和代码都萜出来
Dim i As Integer, tmp As String, n
If Len(strs) Mod 2 Then Exit Function
For i = 1 To Len(strs) Step 2
n = Val("&H" & Mid(strs, i, 2))
If n < 0 Or n > 127 Then
n = Val("&H" & Mid(strs, i, 4))
i = i + 2
End If
tmp = tmp & Chr(n)
Next i
HexToStr = tmp
End FunctionPrivate Sub Command1_Click()
Text2.Text = HexToStr(Text1.Text)
End SubPrivate Sub Form_Load()
Text1.Text = "E3809053696D656EE38091E78E89E69E973C3C3C2A41
2E6C7379E68898E9989FE8BF9EE8B7B3E69C8DE58AA1E599A8"
End Sub
如上代码,只能转出乱码
“銆怱imen銆戠帀鏋?<<*A.lsy鎴橀槦杩炶烦链嶅姟鍣”有人说这个字符串要这么转“它的编码是UTF8的Unicode字符,这个
想还原字符就有些麻烦了,首先从16进制转为UTF8格式,然后转为
数值,最后转ASCII,还原后的字符串为结果”具体操作怎么做?谢谢。希望把转出来的结果和代码都萜出来
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Const CP_UTF8 = 65001
Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Public Function UTF8_Encode(ByVal strUnicode As String) As Byte()
'UTF-8 编码 Dim TLen As Long
Dim lngBufferSize As Long
Dim lngResult As Long
Dim bytUtf8() As Byte
TLen = Len(strUnicode)
If TLen = 0 Then Exit Function
lngBufferSize = TLen * 3 + 1
ReDim bytUtf8(lngBufferSize - 1)
lngResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strUnicode), TLen, bytUtf8(0), lngBufferSize, vbNullString, 0)
If lngResult <> 0 Then
lngResult = lngResult - 1
ReDim Preserve bytUtf8(lngResult)
End If
UTF8_Encode = bytUtf8
End FunctionPublic Function UTF8_Decode(ByRef bUTF8() As Byte) As String
'UTF-8 解码
Dim lRet As Long
Dim lLen As Long
Dim lBufferSize As Long
Dim sBuffer As String
Dim bBuffer() As Byte
lLen = UBound(bUTF8) + 1
If lLen = 0 Then Exit Function
lBufferSize = lLen * 2
sBuffer = String$(lBufferSize, Chr(0))
lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(bUTF8(0)), lLen, StrPtr(sBuffer), lBufferSize)
If lRet <> 0 Then
sBuffer = Left(sBuffer, lRet)
End If
UTF8_Decode = sBuffer
End FunctionPublic Function CreateStringFromByte(ByRef byteArray() As Byte, ByVal ByteLength As Long) As String
'字节数组中的数据连接成字符串 Dim StringData As String
'** 分配字符串空间
StringData = Space(ByteLength)
'** 复制字符数组地址内容到字符串地址
MoveMemory ByVal StringData, ByVal VarPtr(byteArray(0)), ByteLength
'** 返回字符串
CreateStringFromByte = StringData
End FunctionPublic Function SaveStringToByteArry(ByRef strString As String) As Byte()
'把字符串存入字节数组
Dim BytArray() As Byte, lngStrLen As Long
'** 获取字符串的长度(字节)
lngStrLen = LenB(StrConv(strString, vbFromUnicode))
'** 分配数组空间
ReDim BytArray(lngStrLen - 1) '** 将字符串地址中的内容拷贝到数组
MoveMemory ByVal VarPtr(BytArray(0)), ByVal strString, lngStrLen
SaveStringToByteArry = BytArray
End Function这是我写War3改键器搜的UTF-8编解码模块
但是我不知道怎么用这些函数过程。
我有点不明白。- -能不能直接把完整代码萜给我。谢谢。
Dim s As String
s = "E3809053696D656EE38091E78E89E69E973C3C3C2A412E6C7379E68898E9989FE8BF9EE8B7B3E69C8DE58AA1E599A8"
If Len(s) Mod 2 <> 0 Then s = "0" & s
Dim arr() As Byte
ReDim arr(Len(s) \ 2 - 1) As Byte
Dim xx As Long
For xx = 0 To Len(s) \ 2 - 1
arr(xx) = "&h" & Mid(s, (xx * 2) + 1, 2)
Next
Debug.Print UTF8_Decode(arr)
End Sub