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

Published on 12 May 2024 09:40 PM

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

Source code in the autohotkey programming language

MsgBox % number2base(200, 16) ; 12
MsgBox % parse(200, 16)  ; 512

number2base(number, base)
{
  While, base < digit := floor(number / base)
  {
    result := mod(number, base) . result
    number := digit
  }
  result := digit . result
  Return result
}

parse(number, base)
{
  result = 0
  pos := StrLen(number) - 1
  Loop, Parse, number 
  {
    result := ((base ** pos) * A_LoopField) + result
    base -= 1
  }
  Return result
}


MsgBox % ToBase(29,3)
MsgBox % ToBase(255,16)

MsgBox % FromBase("100",8)
MsgBox % FromBase("ff",16)

ToBase(n,b) { ; n >= 0, 1 < b <= 36
   Return (n < b ? "" : ToBase(n//b,b)) . ((d:=mod(n,b)) < 10 ? d : Chr(d+87))
}

FromBase(s,b) { ; convert base b number s=strings of 0..9,a..z, to AHK number
   Return (L:=StrLen(s))=0 ? "":(L>1 ? FromBase(SubStr(s,1,L-1),b)*b:0) + ((c:=Asc(SubStr(s,0)))>57 ? c-87:c-48)
}


  

You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Population count step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Neko programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Scala programming language