How can I convert a long integer to binary? For example, I want the integer 725 to show up as 1011010101 in another field in the table. Each of the bits in the byte are representative of the state of a different piece of machinery.
Convert Long Integer to Binary byte
Collapse
X
-
A quick Google search returned this: VBA - Converting Between Decimal and Binary. One thing you should note, the binary "number" is actually a string data type, so if you are storing the binary data in a table, the field will need to be Text. -
Here you go:
Parameter 1 to is the Decimal value, Parameter 2 is how many place holders you would like.Code:Public Function getBinary(ByRef iDec As Integer, ByRef iResolution As Integer) As String Dim iCount As Integer getBinary = "" For iCount = iResolution - 1 To 0 Step -1 getBinary = getBinary + IIf(iDec And (2 ^ iCount), "1", "0") Next iCount End Function
This is just the function, you may need to resort to a query to get this returned to you on every line in the table.Comment
-
I don't believe there is an inbuilt Binary() function like the Hex() function.
Here's a function you can include in your project that should do a similar job. You may decide you need to be able to specify the number of digits. If so that's pretty easy to handle.
Code:Public Function DispBinary(ByVal lngVar As Long) As String Do While lngVar > 0 DispBinary = Format(lngVar And &H01, "0") & DispBinary lngVar = Int(lngVar / 2) Loop End FunctionComment
Comment