vb 十六进制转字符串

gmyhbio 2009-09-25 08:15:43
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 Function

Private Sub Command1_Click()
Text2.Text = HexToStr(Text1.Text)
End Sub

Private Sub Form_Load()
Text1.Text = "E3809053696D656EE38091E78E89E69E973C3C3C2A41
2E6C7379E68898E9989FE8BF9EE8B7B3E69C8DE58AA1E599A8"
End Sub




如上代码,只能转出乱码
“銆怱imen銆戠帀鏋?<<*A.lsy鎴橀槦杩炶烦链嶅姟鍣”

有人说这个字符串要这么转“它的编码是UTF8的Unicode字符,这个
想还原字符就有些麻烦了,首先从16进制转为UTF8格式,然后转为
数值,最后转ASCII,还原后的字符串为结果”

具体操作怎么做?谢谢。希望把转出来的结果和代码都萜出来
...全文
1592 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
rishen1 2010-09-15
  • 打赏
  • 举报
回复
怎么看不到部分回复?
MORHH 2009-11-09
  • 打赏
  • 举报
回复
没弄过
三楼の郎 2009-09-26
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
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
gmyhbio 2009-09-26
  • 打赏
  • 举报
回复
alifriend你是结果是正确的。
但是我不知道怎么用这些函数过程。
我有点不明白。- -能不能直接把完整代码萜给我。谢谢。
嗷嗷叫的老马 2009-09-26
  • 打赏
  • 举报
回复
.................帮顶
波导终结者 2009-09-25
  • 打赏
  • 举报
回复
Option Explicit

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 String, ByVal lpUsedDefaultChar As Long) As Long
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 Function

Public 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 Function

Public 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 Function

Public 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编解码模块
波导终结者 2009-09-25
  • 打赏
  • 举报
回复
【Simen】玉林<<<*A.lsy战队连跳服务器
slowgrace 2009-09-25
  • 打赏
  • 举报
回复
我转出来的是“【Simen】玉?<<*A.lsy战队连跳服务 ”

你确定你的编码没问题么?
贝隆 2009-09-25
  • 打赏
  • 举报
回复
感觉你的代码不对,研究一下
threenewbee 2009-09-25
  • 打赏
  • 举报
回复
http://www.google.cn/search?hl=zh-CN&source=hp&q=ado.stream+utf-8+byval&aq=f&oq=

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧