How to resolve the algorithm Non-decimal radices/Convert step by step in the Liberty BASIC programming language
How to resolve the algorithm Non-decimal radices/Convert step by step in the Liberty BASIC programming language
Table of Contents
Problem Statement
Number base conversion is when you express a stored integer in an integer base, such as in octal (base 8) or binary (base 2). It also is involved when you take a string representing a number in a given base and convert it to the stored integer form. Normally, a stored integer is in binary, but that's typically invisible to the user, who normally enters or sees stored integers as decimal.
Write a function (or identify the built-in function) which is passed a non-negative integer to convert, and another integer representing the base. It should return a string containing the digits of the resulting number, without leading zeros except for the number 0 itself. For the digits beyond 9, one should use the lowercase English alphabet, where the digit a = 9+1, b = a+1, etc. For example: the decimal number 26 expressed in base 16 would be 1a. Write a second function which is passed a string and an integer base, and it returns an integer representing that string interpreted in that base. The programs may be limited by the word size or other such constraint of a given language. There is no need to do error checking for negatives, bases less than 2, or inappropriate digits.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Non-decimal radices/Convert step by step in the Liberty BASIC programming language
Source code in the liberty programming language
' 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
You may also check:How to resolve the algorithm Logical operations step by step in the Self programming language
You may also check:How to resolve the algorithm Elliptic curve arithmetic step by step in the Phix programming language
You may also check:How to resolve the algorithm DNS query step by step in the Neko programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the Retro programming language
You may also check:How to resolve the algorithm Fivenum step by step in the RPL programming language