Non-decimal radices/Convert: Difference between revisions
Content added Content deleted
(GP) |
({{header|Liberty BASIC}}) |
||
Line 697: | Line 697: | ||
//optional special case for hex: |
//optional special case for hex: |
||
i = +('0x'+s) //hexadecimal base 16, if s='1a' then i=26.</lang> |
i = +('0x'+s) //hexadecimal base 16, if s='1a' then i=26.</lang> |
||
=={{header|Liberty BASIC}}== |
|||
<lang lb> ' 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. |
|||
maxIntegerBitSize =len( str$( number)) |
|||
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 |
|||
</lang> |
|||
=={{header|M4}}== |
=={{header|M4}}== |