[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$ = ""
h$ = HEX$(decimal&)
FOR re = 1 TO LEN(h$)
dig = INSTR("0123456789ABCDEF", MID$(h$, re, 1)) - 1
IF dig < 0 THEN Bn$ = "": 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....
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.