Solved

Decimal to Binary and Vice-Versa in VB6

Posted on 2002-04-24
10
1,462 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
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.

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
ID: 6966756
sorry, disregard everything above "checkout:" in the above comment
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6966836
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 16

Expert Comment

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

Expert Comment

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

Expert Comment

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

Expert Comment

by:Richie_Simonetti
ID: 6966908
0
 
LVL 22

Accepted Solution

by:
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

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
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.
0
 
LVL 22

Expert Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 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