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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Decimal ----> Binary Decimal <-------- Binary

Status
Not open for further replies.

KenshinHimura

Programmer
May 8, 2003
91
US
I need help making a function that converts decimal to binary and vice versa.
 
FUNCTION Bin% (Bit$)
Temp& = 0
Length% = LEN(Bit$)
FOR X% = 1 TO Length%
IF MID$(Bit$, Length% - X% + 1, 1) = "1" THEN
Temp& = Temp& + 2 ^ (X% - 1)
END IF
NEXT X%
IF Temp& > 32767 THEN
Bin% = Temp& - 65536
ELSE
Bin% = Temp&
END IF
END FUNCTION

FUNCTION BinDec& (Binary$) STATIC
Decimal& = 0
Power% = 0
Binary$ = UCASE$(Binary$)
FOR I% = LEN(Binary$) TO 1 STEP -1
Digit% = ASC(MID$(Binary$, I%, 1)) - 48
IF Digit% < 0 OR Digit% > 1 THEN
Decimal& = 0
EXIT FOR
END IF
Decimal& = Decimal& + Digit% * 2 ^ (Power%)
Power% = Power% + 1
NEXT I%
BinDec& = Decimal&
END FUNCTION
 
BinDec converts binary to decimal, but what does Bin do? And do you convert Decimal to Binary?
 
It converts a binary string to a decimal
if
Bin%(101101) will come out to 45
 
It seems that BinDec converts binary to decimal, and Bin does too? Anyway I was looking through an ASM tutorial and it explained the conversions binry to decimal and decimal to binary so i wrote my own functions because i could only get your BinDec to work. Thanx for your help. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top