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

Published on 12 May 2024 09:40 PM

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

Source code in the action! programming language

CHAR ARRAY digits="0123456789abcdefghijklmnopqrstuvwxyz"

PROC CheckBase(BYTE b)
  IF b<2 OR b>digits(0) THEN
    PrintE("Base is out of range!")
    Break()
  FI
RETURN

PROC Encode(CARD v BYTE b CHAR ARRAY s)
  CARD d
  BYTE i,len
  CHAR tmp

  CheckBase(b)
  len=0
  DO
    d=v MOD b
    len==+1
    s(len)=digits(d+1)
    v==/b
  UNTIL v=0
  OD
  s(0)=len

  FOR i=1 to len/2
  DO
    tmp=s(i)
    s(i)=s(len-i+1)
    s(len-i+1)=tmp
  OD
RETURN

CARD FUNC Decode(CHAR ARRAY s BYTE b)
  CARD res
  BYTE i,j,found

  CheckBase(b)
  res=0
  FOR i=1 TO s(0)
  DO
    found=0
    FOR j=1 TO digits(0)
    DO
      IF digits(j)=s(i) THEN
        found=1 EXIT
      FI
    OD
    IF found=0 THEN
      PrintE("Unrecognized character!")
      Break()
    FI
    res==*b
    res==+j-1
  OD
RETURN (res)

PROC Main()
  CARD v=[6502],v2
  BYTE b
  CHAR ARRAY s(256)

  FOR b=2 TO 23
  DO
    Encode(v,b,s)
    v2=Decode(s,b)
    PrintF("%U -> base %B %S -> %U%E",v,b,s,v2)
  OD
RETURN

  

You may also check:How to resolve the algorithm Count in octal step by step in the DCL programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Perl programming language
You may also check:How to resolve the algorithm Stable marriage problem step by step in the D programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Forth programming language