Decimal to Binary and Vice-Versa in VB6

Posted on 2002-04-24
Last Modified: 2010-05-18
I have some data that is unfortunately returned by a DLL that i call only in decimal, hexa, or string format.
I need to process it in the basic binary format.
So, i need the basic binary like 10110010 .
How do I convert it from dec to bin and vice-versa in Visual Basic 6? I didn't find a function for that in MSDN.

Further details:
I a writing a program that would like to be a software oscilloscope for a custom-made data aquisition card. This card is connected to the parallel port(LPT) and I am accessing it from Windows NT through the DriverLINX Port I/O Driver for Win95 and WinNT DLL from
When reading a certain address from the LPT, a dec, hex, or string of these is returned. And I need the bin to see if a certain pin from the LPT is 0 or 1(high, or low)

Thank you very much.
Question by:robertut
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
LVL 18

Expert Comment

ID: 6966754
decimal to binary:

A quick and easy function to return the binary value for a number, without any preceding "0"s. This function returns a string with the binary number.


click on the "Base Conversion" link

LVL 18

Expert Comment

ID: 6966756
sorry, disregard everything above "checkout:" in the above comment
LVL 16

Expert Comment

ID: 6966836
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

LVL 16

Expert Comment

ID: 6966899
Check this:
Function Pow(nNumber As Integer, ipower As Integer) As Integer
    Pow = nNumber ^ ipower
End Function
Private Sub Command1_Click()
Dim llen As Integer, idx As Integer, iPow As Integer
Dim iCount As Integer
llen = Len(Text1.Text)
For idx = llen To 1 Step -1
    iPow = iPow + (Mid$(Text1.Text, idx, 1) * Pow(2, iCount))
    iCount = iCount + 1
Next idx
MsgBox iPow
End Sub
LVL 16

Expert Comment

ID: 6966902
Sorry, that was for binary to decimal.
LVL 16

Expert Comment

ID: 6966905
That code was developed based on:
LVL 16

Expert Comment

ID: 6966908
LVL 22

Accepted Solution

rspahitz earned 50 total points
ID: 6967002
Although the answer you seek may be in the above comments, this will only apply if the binary number is coming in as a string.

Based on your question, I'm wondering if those numbers are actually coming in a numbers, in which case binary number is only binary because that's how you interpret it; the same in hexadecimal.

For example, the number 10 is really the concept of this many things: ||||| |||||
You can interpret it as 1 group of ten things, two groups of 5 things or five groups of two things, etc.

You can also represent it as "A" things in hexadecimal or "1010" things in binary.

Internally, the computer stores everything in binary; VB merely interprets it according to a few allowable representations.

Therefore, in the computer, the only difference between a binary number, hexadecimal number, and decimal number is in how they are displayed.  When a dll wants a number, it doesn't care about the display; only the internal value.

When you say, "When reading a certain address from the LPT, a bin, hex, or string is returned" does that mean that the tool is returning a string of binary digits or a string of hex digits, or a string of something else?  If it's returning numbers, then a binary number and a decimal number are really the same thing.

>I need the bin to see if a certain pin from the LPT is 0 or 1(high, or low)

You can do this with a decimal number since it's really the same number:

(DecNum AND 1) returns 0 or 1 for the 0th bit (from the right)
(DecNum AND 2) returns 0 or 2 for the 1st bit
(DecNum AND 4) returns 0 or 4 for the 2nd bit
(DecNum AND 8) returns 0 or 8 for the 3rd bit
(DecNum AND 16) returns 0 or 16 for the 4th bit
(DecNum AND 32) returns 0 or 32 for the 5th bit
(DecNum AND 64) returns 0 or 64 for the 6th bit
(DecNum AND 128) returns 0 or 128 for the 7th bit

(If DecNum is 10)
debug.print DecNum AND 1 ' returns 0
debug.print DecNum AND 2 ' returns 2
debug.print DecNum AND 4 ' returns 0
debug.print DecNum AND 8 ' returns 8

Author Comment

ID: 6967865
Thank you for your quick responses.
When I came back and saw the responses, I also read my question, adn discovered that I mistakenly wrote:

"When reading a certain address from the LPT, a BIN, hex, or string is returned" and "I have some data that is unfortunately returned by a DLL that i call only in BINARY, hexa, or string format." while what I really meant was:
"When reading a certain address from the LPT, a DEC, hex, or string is returned" and "I have some data that is unfortunately returned by a DLL that i call only in DECIMAL, hexa, or string format."

Anyway, still good answers came. Thak you.

for rspahitz:
the tool returns a value, not a string of deciaml digits, nor a string of hex digits, nor a string  of something else. It is my option to choose what I want it to return: Decimal, Hexa or a String of these.
Since I needed the binary value, I asked if maybe I could covert any of the above in binaries.
I need to test the presence of a low and immediately changing to high on a pin of the LPT in one part of the program; but later on I need the binary values.
I am designing this program for the old type of LPT, which is not bi-directional, that means that it cannot read 8 bits simultaneously. Thus my hardware has a multiplexor that allows to split the 8-bit number retuned by the A/D convertor in two 4-bit numbers sent in sequentially. Later I have to re-create the 8 bit from these two 4 bits(this will be a whole other question), convert it to long and display it on the chart.
Thank you.
LVL 22

Expert Comment

ID: 6969190
So it sounds like my example above should help against each of the two 4-bit numbers.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question