How to resolve the algorithm Non-decimal radices/Convert step by step in the Arturo programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Non-decimal radices/Convert step by step in the Arturo 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 Arturo programming language

Source code in the arturo programming language

fromBase: function [x,base][
    if base=2 [ return from.binary x ]
    if base=8 [ return from.octal x ]
    if base=16 [ return from.hex x ]

    return to :integer x
]

toBase: function [x,base][
    if base=2 [ return as.binary x ]
    if base=8 [ return as.octal x ]
    if base=16 [ return as.hex x ]

    return to :string x
]

loop 1..20 'i ->
    print [
        i "base2:" toBase i 2 "base8:" toBase i 8 "base16:" toBase i 16
    ]

print ""

print ["101 => from base2:" fromBase "101" 2 "from base8:" fromBase "101" 8 "from base16:" fromBase "101" 16]
print ["123 => from base8:" fromBase "123" 8 "from base16:" fromBase "123" 16]
print ["456 => from base8:" fromBase "456" 8 "from base16:" fromBase "456" 16]


  

You may also check:How to resolve the algorithm Substring/Top and tail step by step in the BQN programming language
You may also check:How to resolve the algorithm Largest number divisible by its digits step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the GDScript programming language
You may also check:How to resolve the algorithm AVL tree step by step in the Ada programming language
You may also check:How to resolve the algorithm Unicode strings step by step in the Nemerle programming language