If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > Binary Representation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-28-04, 09:59
tb_vball tb_vball is offline
Registered User
 
Join Date: May 2004
Posts: 18
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
Reply With Quote
  #2 (permalink)  
Old 06-29-04, 09:51
SCIROCCO SCIROCCO is offline
Registered User
 
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!
Reply With Quote
  #3 (permalink)  
Old 06-29-04, 10:31
tb_vball tb_vball is offline
Registered User
 
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?
Reply With Quote
  #4 (permalink)  
Old 06-30-04, 08:48
SCIROCCO SCIROCCO is offline
Registered User
 
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!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On