How to resolve the algorithm Non-decimal radices/Input step by step in the PARI/GP programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Non-decimal radices/Input step by step in the PARI/GP programming language

Table of Contents

Problem Statement

It is common to have a string containing a number written in some format, with the most common ones being decimal, hexadecimal, octal and binary. Such strings are found in many places (user interfaces, configuration files, XML data, network protocols, etc.) This task requires parsing of such a string (which may be assumed to contain nothing else) using the language's built-in facilities if possible. Parsing of decimal strings is required, parsing of other formats is optional but should be shown (i.e., if the language can parse in base-19 then that should be illustrated). The solutions may assume that the base of the number in the string is known. In particular, if your language has a facility to guess the base of a number by looking at a prefix (e.g. "0x" for hexadecimal) or other distinguishing syntax as it parses it, please show that. The reverse operation is in task Non-decimal radices/Output For general number base conversion, see Non-decimal radices/Convert.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Non-decimal radices/Input step by step in the PARI/GP programming language

Source code in the pari/gp programming language

convert(numb1,b1,b2)={
  my(B=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],a=0,c="");
  numb1=Vec(Str(numb1));
  forstep(y=#numb1,1,-1,
    for(x=1,b1,
      if(numb1[y]==B[x],
        a=a+(x-1)*b1^(#numb1-y)
      )
    )
  );
  until(a/b2==0,
    c=concat(B[a%b2+1],c);
    a=a\b2
  );
  c
};

fromdigits([1,15,15],16)

  

You may also check:How to resolve the algorithm URL encoding step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Long multiplication step by step in the Fortran programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the Stata programming language
You may also check:How to resolve the algorithm Create a two-dimensional array at runtime step by step in the Pop11 programming language
You may also check:How to resolve the algorithm OpenGL step by step in the Mathematica / Wolfram Language programming language