Option ExplicitPrivate Sub Command1_Click() Dim s As String s = String2Hex("今天abc123") MsgBox s s = Hex2String(s) MsgBox s End SubPrivate Function String2Hex(ByVal s As String) As String On Error Resume Next Dim bytearr() As Byte bytearr = StrConv(s, vbFromUnicode) Dim temps As String Dim temp As Byte Dim i As Long Dim outs As String For i = 0 To UBound(bytearr) temp = bytearr(i) temps = Hex(temp) temps = Right("00" + temps, 2) outs = outs + temps Next String2Hex = outs End Function Private Function Hex2String(ByVal s As String) As String On Error Resume Next Dim bytearr() As Byte Dim temps As String Dim temp As Byte Dim i As Long Dim j As Long j = 0 Dim outs As String For i = 1 To Len(s) Step 2 temps = Mid(s, i, 2) temp = Val("&H" & temps) ReDim Preserve bytearr(j) bytearr(j) = temp j = j + 1 Next outs = StrConv(bytearr, vbUnicode) Hex2String = outs End Function
楼上正解。Function tohanzi(ByVal data As String) As StringDim i As Long For i = 1 To Len(data) Step 4 tohanzi = tohanzi & Chr("&H" & Mid(data, i, 2)) Next End FunctionPrivate Sub Command2_Click() Const data = "B2D2C2AE" MsgBox tohanzi(data) End Sub
Function tohanzi(ByVal data As String) As StringDim i As Long For i = 1 To Len(data) Step 4 tohanzi = tohanzi & Chr("&H" & Mid(data, i, 4)) Next End FunctionPrivate Sub Command2_Click() Const data = "B2D2C2AE" MsgBox tohanzi(data) End Sub
Dim s As String
s = String2Hex("今天abc123")
MsgBox s
s = Hex2String(s)
MsgBox s
End SubPrivate Function String2Hex(ByVal s As String) As String
On Error Resume Next
Dim bytearr() As Byte
bytearr = StrConv(s, vbFromUnicode)
Dim temps As String
Dim temp As Byte
Dim i As Long
Dim outs As String
For i = 0 To UBound(bytearr)
temp = bytearr(i)
temps = Hex(temp)
temps = Right("00" + temps, 2)
outs = outs + temps
Next
String2Hex = outs
End Function
Private Function Hex2String(ByVal s As String) As String
On Error Resume Next
Dim bytearr() As Byte
Dim temps As String
Dim temp As Byte
Dim i As Long
Dim j As Long
j = 0
Dim outs As String
For i = 1 To Len(s) Step 2
temps = Mid(s, i, 2)
temp = Val("&H" & temps)
ReDim Preserve bytearr(j)
bytearr(j) = temp
j = j + 1
Next
outs = StrConv(bytearr, vbUnicode)
Hex2String = outs
End Function
For i = 1 To Len(data) Step 4
tohanzi = tohanzi & Chr("&H" & Mid(data, i, 2))
Next
End FunctionPrivate Sub Command2_Click()
Const data = "B2D2C2AE"
MsgBox tohanzi(data)
End Sub
For i = 1 To Len(data) Step 4
tohanzi = tohanzi & Chr("&H" & Mid(data, i, 4))
Next
End FunctionPrivate Sub Command2_Click()
Const data = "B2D2C2AE"
MsgBox tohanzi(data)
End Sub