如何将二进制字符串转换为byte
2016-03-27
展开全部
Byte数据可通过一定的自定义函数过程,将其转化成二进制字符串。
可先将Byte数据其10进制值用Hex函数转为16进制的字符串,然后通过自定义函数转成二进制字符串。
Hex 函数,返回代表十六进制数值的 String。
以下是VB6.0的处理代码。
Option Explicit
Dim sBin
Dim sHex
Dim BinData
Const s1 = "0000101001101111000", s2 = "0125A4936DB7FEC8"
Dim yTemp() As Byte
Private Sub Command1_Click()
If Val(Text1) < 128 Then
sHex = Right("0" & Hex(Text1), 2)
Call HexToBin(sHex)
Text2 = BinData
Else
sHex = Hex(Text1)
Call HexToBin(sHex)
Text2 = BinData
End If
End Sub
Public Function HexToBin(ByVal sHex As String) As String
Dim i As Integer, sBin As String
sHex = UCase(sHex)
For i = 1 To Len(sHex)
sBin = sBin & Mid(s1, InStr(1, s2, Mid(sHex, i, 1)), 4)
Next i
HexToBin = sBin
BinData = sBin
End Function
Private Sub Form_Load()
Text1 = ""
Text2 = ""
End Sub
在Text1中输入Byte数据的10进制值,Text2就展现其二进制字符串。
可先将Byte数据其10进制值用Hex函数转为16进制的字符串,然后通过自定义函数转成二进制字符串。
Hex 函数,返回代表十六进制数值的 String。
以下是VB6.0的处理代码。
Option Explicit
Dim sBin
Dim sHex
Dim BinData
Const s1 = "0000101001101111000", s2 = "0125A4936DB7FEC8"
Dim yTemp() As Byte
Private Sub Command1_Click()
If Val(Text1) < 128 Then
sHex = Right("0" & Hex(Text1), 2)
Call HexToBin(sHex)
Text2 = BinData
Else
sHex = Hex(Text1)
Call HexToBin(sHex)
Text2 = BinData
End If
End Sub
Public Function HexToBin(ByVal sHex As String) As String
Dim i As Integer, sBin As String
sHex = UCase(sHex)
For i = 1 To Len(sHex)
sBin = sBin & Mid(s1, InStr(1, s2, Mid(sHex, i, 1)), 4)
Next i
HexToBin = sBin
BinData = sBin
End Function
Private Sub Form_Load()
Text1 = ""
Text2 = ""
End Sub
在Text1中输入Byte数据的10进制值,Text2就展现其二进制字符串。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询