Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting binary to decimal 1

Status
Not open for further replies.

fugigoose

Programmer
Jun 20, 2001
241
US
How do you convert a binary number to a decimal number. For instance, '10101100' to a regular decimal.
 
[tt]
' Declare the functions
DECLARE FUNCTION Bin$ (decimal&)
DECLARE FUNCTION Dec& (Binar$)
CLS

' Convert 255 into "11111111"
alt$ = Bin$(255)
PRINT alt$

' Convert "11111111" back to 255
PRINT Dec&(alt$)


FUNCTION Dec& (Binary$)

' Convert a binary string to a decimal number...
decimal& = 0: power = 0
Binary$ = UCASE$(Binary$)
FOR re = LEN(Binary$) TO 1 STEP -1
dig = ASC(MID$(Binary$, re, 1)) - 48
IF dig < 0 OR dig > 1 THEN decimal& = 0: EXIT FOR
decimal& = decimal& + dig * 2 ^ (power)
power = power + 1
NEXT
Dec& = decimal&
END FUNCTION

FUNCTION Bin$ (decimal&)

' Convert a long integer to a binary string...
Bn$ = &quot;&quot;
h$ = HEX$(decimal&)
FOR re = 1 TO LEN(h$)
dig = INSTR(&quot;0123456789ABCDEF&quot;, MID$(h$, re, 1)) - 1
IF dig < 0 THEN Bn$ = &quot;&quot;: EXIT FOR
j = 8
k = 4
DO
Bn$ = Bn$ + RIGHT$(STR$((dig \ j) MOD 2), 1)
j = j - (j \ 2)
k = k - 1
IF k = 0 THEN EXIT DO
LOOP WHILE j
NEXT
Bin$ = Bn$
END FUNCTION
[/tt]

That's one way to do it....


VCA.gif
 
Thanx. Thats kind of the way i did it in Visual Basic but i wasnt sure of some of the BASIC commands. I'm using BASIC because of the ease of robotic interface using the OUT command. PAralell port has 8 pins and the OUT command uses a decimal so binary conversion is key.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top