这里是一个10进数据转换成二进制字符串的例子 Private Sub Command1_Click() Dim b As Integer, s As String, i As Integer b = 143 For i = 1 To 8 b = b * 2 If b >= 256 Then b = b - 256 s = s & "1" Else s = s & "0" End If Next Print s End Sub
方法一:Dim tmp As Byte Dim i As Integer, n As Integertmp = &H8FFor i = 7 To 0 Step -1 n = 2 ^ i Debug.Print (tmp And n) / n Next i
方法二:Dim tmp As Byte, i As Integer Dim strHex As String, strBinary As String
tmp = &H8FstrHex = Format(Hex(tmp), "00") For i = 1 To 2 strBinary = strBinary & Choose(Val("&H" & Mid(strHex, i, 1)) + 1, "0000", "0001", "0010", "0011", _ "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111") Next iFor i = 1 To 8 Debug.Print Mid(strBinary, i, 1) Next i
Dim b As Integer, s As String, i As Integer b = &H8F For i = 7 To 0 Step -1 s = s & (b And 2 ^ i) / 2 ^ i Next i Print s
'打印为1的位置 Dim str As String Dim btmp() As Byte Dim i As Long
str = "10001111" btmp = str
For i = 0 To UBound(btmp) Step 2 If Chr(btmp(i)) Then Debug.Print CStr(i / 2 + 1) & "-" & Chr(btmp(i)) End If Next
Function BinByte(ByVal Value As Byte) As String Dim s As String Dim i As Long Dim n As Byte
n = Value s = Space$(8) For i = 8 To 1 Step -1 Mid$(s, i, 1) = CStr(n Mod 2) n = n \ 2 Next
如果=0 则表示 第n位 =0
Private Sub Command1_Click()
Dim b As Integer, s As String, i As Integer
b = 143
For i = 1 To 8
b = b * 2
If b >= 256 Then
b = b - 256
s = s & "1"
Else
s = s & "0"
End If
Next
Print s
End Sub
方法一:Dim tmp As Byte
Dim i As Integer, n As Integertmp = &H8FFor i = 7 To 0 Step -1
n = 2 ^ i
Debug.Print (tmp And n) / n
Next i
方法二:Dim tmp As Byte, i As Integer
Dim strHex As String, strBinary As String
tmp = &H8FstrHex = Format(Hex(tmp), "00")
For i = 1 To 2
strBinary = strBinary & Choose(Val("&H" & Mid(strHex, i, 1)) + 1, "0000", "0001", "0010", "0011", _
"0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111")
Next iFor i = 1 To 8
Debug.Print Mid(strBinary, i, 1)
Next i
Dim b As Integer, s As String, i As Integer
b = &H8F
For i = 7 To 0 Step -1
s = s & (b And 2 ^ i) / 2 ^ i
Next i
Print s
'打印为1的位置
Dim str As String
Dim btmp() As Byte
Dim i As Long
str = "10001111"
btmp = str
For i = 0 To UBound(btmp) Step 2
If Chr(btmp(i)) Then
Debug.Print CStr(i / 2 + 1) & "-" & Chr(btmp(i))
End If
Next
Dim s As String
Dim i As Long
Dim n As Byte
n = Value
s = Space$(8)
For i = 8 To 1 Step -1
Mid$(s, i, 1) = CStr(n Mod 2)
n = n \ 2
Next
BinByte = s
End Function