Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Decimal to Binary and Vice-Versa in VB6

Posted on 2002-04-24
Medium Priority
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 www.sstnet.com
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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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 150 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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

572 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