Solved

Decimal to Binary and Vice-Versa in VB6

Posted on 2002-04-24
10
1,446 Views
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.
Robert
0
Comment
Question by:robertut
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
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.

checkout:
http://www.codearchive.com/newcode/dataselect/select.cgi?section=numbers&area=vbasic&startat=0

click on the "Base Conversion" link

0
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
sorry, disregard everything above "checkout:" in the above comment
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
Sorry, that was for binary to decimal.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
That code was developed based on:
www.geocities.com/regia_me/bintodec.htm
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 50 total points
Comment Utility
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

Example:
(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
0
 

Author Comment

by:robertut
Comment Utility
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.
0
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
So it sounds like my example above should help against each of the two 4-bit numbers.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) 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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now