Results 1 to 4 of 4
  1. #1
    Join Date
    May 2004
    Posts
    18

    Unanswered: Binary Representation

    Hi;
    does anyone know how to code using binary number in visual basic
    Let say I want to send out a 2 bytes representing number 255

    I know the code for hexadecimal is &HFF
    What is the code for binary?

    Thank you

  2. #2
    Join Date
    Mar 2004
    Location
    www.scirocco.ca
    Posts
    346
    Here is a function that returns the binary representation of an integer:

    Code:
    Private Function Get_Binary(iInComing As Integer) As String
    
    ' ---------------------------------------------------------
    ' Written by Kenneth Ives             kenaso@home.com
    '
    ' convert an integer value to its binary equivilent
    '
    ' ---------------------------------------------------------
    
    ' ---------------------------------------------------------
    ' Define variables
    ' ---------------------------------------------------------
      Dim iDecValue As Integer
      Dim iBitCnt As Integer
      Dim iBitValue As Integer
      Dim iCurrBit As Integer
      Dim sTmp As String
    
    ' ---------------------------------------------------------
    ' Initialize variables
    ' ---------------------------------------------------------
      iDecValue = iInComing
      iBitCnt = 0
      iBitValue = 128
      sTmp = ""
    
    ' ---------------------------------------------------------
    ' Parse the incoming integer and determine which bits
    ' should be turned on
    ' ---------------------------------------------------------
      Do
          ' Increment the bit count
          iBitCnt = iBitCnt + 1
    
          ' divide the input integer by the current
          ' bit value.  Starts at 128
          iCurrBit = Int(iDecValue / iBitValue)
    
          ' if the results are greater than zero then
          ' a "1" is placed in that bit position; otherwise,
          ' a "0" will be placed there
          If iCurrBit > 0 Then
        
              ' append a "1" to the string
              sTmp = sTmp & "1"
    
              ' subtract the current bit value
              ' from the input integer
              iDecValue = iDecValue - iBitValue
          Else
              ' append a "0" to the string
              sTmp = sTmp & "0"
          End If
        
          ' Decrement the Bit value by dividing by 2
          iBitValue = Int(iBitValue / 2)
        
      Loop Until iBitCnt = 8
    
    ' ---------------------------------------------------------
    ' REturn the binary expression
    ' ---------------------------------------------------------
      Get_Binary = sTmp
    
    End Function
    http://www.scirocco.ca/images/banner...occobanner.gif

    Download for FREE the ADO/DAO Data Controls that makes life EASIER developing database applications in: VB, FoxPro, Access, VC++, .NET etc... Navigate, Add New, Delete, Update, Search, Undo and Save your changes. Supports Disconnected Recordsets and Transactions!

    Or try our Ask An Expert service to answer any of your questions!

  3. #3
    Join Date
    May 2004
    Posts
    18
    I am sorry, you might have misunderstood me ...

    what I want is to write a number in binary in visual basic.

    for example, the way to write 255 in hexadecimal is &HFF

    What is the way to write it in Binary?

    The reason is that if I want to send 8 bits word to the parallel port or something, I want to know which one is on and which is off. Binary makes it easy to see...

    So lets say I want to send the binary 11110101, what is the way to write this in VB?

  4. #4
    Join Date
    Mar 2004
    Location
    www.scirocco.ca
    Posts
    346
    I don't think there is a native binary representation in Visual Basic. However you could either use a string or use an array of the data type byte. i.e.

    Dim byteBinary(8) As Byte
    http://www.scirocco.ca/images/banner...occobanner.gif

    Download for FREE the ADO/DAO Data Controls that makes life EASIER developing database applications in: VB, FoxPro, Access, VC++, .NET etc... Navigate, Add New, Delete, Update, Search, Undo and Save your changes. Supports Disconnected Recordsets and Transactions!

    Or try our Ask An Expert service to answer any of your questions!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •