RosettaCodeData/Task/Non-decimal-radices-Convert/Liberty-BASIC/non-decimal-radices-convert.basic
2023-07-01 13:44:08 -04:00

32 lines
1.1 KiB
Text

' Base Converter v6
global alphanum$
alphanum$ ="0123456789abcdefghijklmnopqrstuvwxyz"
for i =1 to 20
RandNum = int( 100 *rnd( 1))
base =2 +int( 35 *rnd( 1))
print "Decimal "; using( "###", RandNum); " to base "; using( "###", base);_
" is "; toBase$( base, RandNum),_
" back to dec. "; toDecimal( base, toBase$( base, RandNum))
next i
end ' ___________________________________________________________
function toBase$( base, number) ' Convert decimal variable to number string.
toBase$ =""
for i =10 to 1 step -1
remainder =number mod base
toBase$ =mid$( alphanum$, remainder +1, 1) +toBase$
number =int( number /base)
if number <1 then exit for
next i
end function
function toDecimal( base, s$) ' Convert number string to decimal variable.
toDecimal =0
for i =1 to len( s$)
toDecimal =toDecimal *base +instr( alphanum$, mid$( s$, i, 1), 1) -1
next i
end function