帮我看一下SimpleBinaryToString这个函数哪里需改进
写了一个函数如下,功能是将二进制数据转换成十六进制,可是有点小问题
比如原本转换完正确的结果应该是4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF...
可是程序转换后结果为: 4D 5A 90 00 30 00 00 04 00 00 00 FF FF
哪位高手帮我看看,程序那个地方有错误,需要怎么修改一下呢
Function SimpleBinaryToString(Binary)
Dim I,S,H
For I = 1 To LenB(Binary)
H = Hex(AscB(MidB(Binary, I, 1)))
If H = "0" Then
H = "0" & H
End If
S = S & H
Next
SimpleBinaryToString = S
End Function
[解决办法]
Public Function BinToHex(ByVal sBin As String) As String
Const s1 = "0000101001101111000", s2 = "0125A4936DB7FEC8"
Dim i As Integer, sHex As String
sBin = String(3 - (Len(sBin) - 1) Mod 4, "0") & sBin
For i = 1 To Len(sBin) Step 4
sHex = sHex & Mid(s2, InStr(1, s1, Mid(sBin, i, 4)), 1)
Next i
BinToHex = sHex
End Function