Convert Long Integer to Binary byte

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pearl341
    New Member
    • Sep 2014
    • 1

    Convert Long Integer to Binary byte

    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.
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    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.

    Comment

    • jforbes
      Recognized Expert Top Contributor
      • Aug 2014
      • 1107

      #3
      Here you go:

      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
      Parameter 1 to is the Decimal value, Parameter 2 is how many place holders you would like.

      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.
      Last edited by jforbes; Sep 12 '14, 03:37 PM. Reason: Clarification

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32661

        #4
        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 Function

        Comment

        Working...