REM Number Conversion
- Binary
-Hex
-Decimal
REM ---------------------------------------
PRINT "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" PRINT "NUMBERICAL DEC-BIN-HEX CONVERSIONS" PRINT "Binary to decimal and hex = b" PRINT "Non-neg integer decimal to binary and hex = i" PRINT "Decimal to binary and hex = d" PRINT "Hex to decimal and binary = h" INPUT "Enter conversion operation (x exits) -> ", op$
i = 0
imax = 0
INPUT "Enter binary number, MSbit entered first -> ", binnum$
warn = 0
IF Bit
(i
) < 48 OR Bit
(i
) > 49 THEN warn
= 1 IF Bit
(i
) = 48 THEN Bit
(i
) = 0 IF Bit
(i
) = 49 THEN Bit
(i
) = 1 PRINT "Entered one or more non-binary bits." PRINT "Entered"; imax;
"-bit binary value (MSbit left) =" INPUT "Is this value correct (y/n)"; ans$
dec = 0
TotDec = 0
dec = Bit(i) * 2 ^ (imax - i)
TotDec = TotDec + dec
PRINT "Value as if integer ="; TotDec
, "Hex value = "; HexNum$
REM PRINT #1, "Entered"; imax;
"-bit binary value (MSbit top) =" REM PRINT #1, "Value as if integer ="; TotDec
, , "Hex value = "; HexNum$
INPUT "Enter value of MSbit (negative to indicate 2's complement) -> ", MSbitVal
TotDecVal = 0
DecVal
= ABS(MSbitVal
) * Bit
(i
) / 2 ^ (i
- 1) TotDecVal = TotDecVal + DecVal
IF MSbitVal
< 0 AND Bit
(1) = 1 THEN TotDecVal
= TotDecVal
- 2 * ABS(MSbitVal
) PRINT "Value of binary number = "; TotDecVal
REM PRINT #1, "MSbit (- for 2's complement) = "; MSbitVal
, "Value of binary number = "; TotDecVal
REM PRINT #1, "----------------------------------------"
warn = 0
INPUT "Enter decimal (non-negative integer) -> ", dec
PRINT "Must enter a non-negative integer." INPUT "Enter hex value (0-9, A-F) -> ", hexnum$
Character
(i
) = ASC(hexnum$
, i
) IF Character
(i
) < 48 THEN warn
= 1 IF Character
(i
) > 57 AND Character
(i
) < 65 THEN warn
= 1 IF Character
(i
) > 70 AND Character
(i
) < 97 THEN warn
= 1 IF Character
(i
) > 102 THEN warn
= 1 PRINT "Entered one or more non-hex characters." dec
= VAL("&h" + hexnum$
) imax = 0
n = 0
numbits = dec / 2 ^ n
n = n + 1
imax = n - 1
PRINT "Decimal value ="; dec
, "Hex value = ";
UCASE$(hexnum$
) PRINT "Requires"; imax
+ 1;
"-bit binary value (MSbit left) =" REM PRINT #1, "Requires"; imax
+ 1;
"-bit binary value (MSbit top) =" remainder = dec / 2 ^ i
Bit(i) = 1
dec = dec - 2 ^ i
Bit(i) = 0
PRINT "MSbit value if non-negative integer =";
2 ^ imax
REM PRINT #1, "MSbit value if non-negative integer =";
2 ^ imax
INPUT "Enter value of MSbit (negative to indicate 2's complement) -> ", MSbitVal
TotDecVal = 0
DecVal
= ABS(MSbitVal
) * Bit
(imax
- i
) / 2 ^ i
TotDecVal = TotDecVal + DecVal
IF MSbitVal
< 0 THEN TotDecVal
= TotDecVal
- 2 * ABS(MSbitVal
) PRINT "Value of binary number = "; TotDecVal
REM PRINT #1, "MSbit (- for 2's complement) = "; MSbitVal
, "Value of binary number = "; TotDecVal
REM PRINT #1, "----------------------------------------"
4 INPUT "Enter decimal value (negative for 2's complement) -> ", decin
INPUT "Enter number of bits in binary value (positive integer) -> ", imax
INPUT "Enter value of the MSbit (positive number) -> ", MSbitVal
dec = MSbitVal * 2 + decin
n = 0
PRINT "Decimal value = "; decin
, "Decimal as positive number ="; dec
PRINT "MSbit value ="; MSbitVal;
"of the"; imax;
"-bit binary number" REM PRINT #1, "Decimal value = "; decin
, "Decimal as positive number ="; dec
REM PRINT #1, "MSbit value ="; MSbitVal;
"of the"; imax;
"-bit binary number" PRINT "Binary value (MSbit left) =" REM PRINT #1, "Binary value (MSbit top) =" subtract(i) = MSbitVal / 2 ^ i
IF dec
- subtract
(i
) >= 0 THEN Bit(i) = 1
dec = dec - subtract(i)
REM Now calculate the actual value produced by the
binary number
TotDecVal = 0
DecVal = MSbitVal * Bit(i) / 2 ^ i
TotDecVal = TotDecVal + DecVal
IF decin
< 0 THEN TotDecVal
= TotDecVal
- 2 * MSbitVal
MSbitIntVal = 2 ^ (imax - 1)
TotIntVal = 0
IntVal = MSbitIntVal * Bit(i) / 2 ^ i
TotIntVal = TotIntVal + IntVal
PRINT "Actual decimal equivalent = "; TotDecVal
, "Positive Integer ="; TotIntVal
, "Hex = ";
HEX$(TotIntVal
) REM PRINT #1, "Actual decimal equivalent = "; TotDecVal
, "Positive Integer ="; TotIntVal
, "Hex = ";
HEX$(TotIntVal
) REM PRINT #1, "----------------------------------------"