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

Published on 12 May 2024 09:40 PM

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

Source code in the freebasic programming language

' FB 1.05.0 Win64

Function min(x As Integer, y As Integer) As Integer
  Return IIf(x < y, x, y)
End Function

Function convertToBase (n As UInteger, b As UInteger) As String  
  If n < 2 OrElse b < 2 OrElse b = 10 OrElse b > 36 Then Return Str(n)
  Dim result As String = "" 
  Dim digit As Integer
  While n > 0
    digit = n Mod b
    If digit < 10 Then
      result = digit & result
    Else
      result = Chr(digit + 87) + result
    End If
     n \= b
  Wend
  Return result
End Function

Function convertToDecimal (s As Const String, b As UInteger) As UInteger
  If b < 2 OrElse b > 36 Then Return 0
  Dim t As String = LCase(s)
  Dim result As UInteger = 0
  Dim digit As Integer
  Dim multiplier As Integer = 1
  For i As Integer = Len(t) - 1 To 0 Step - 1
     digit = -1
     If t[i] >= 48 AndAlso t[i] <= min(57, 47 + b) Then
       digit = t[i] - 48
     ElseIf b > 10 AndAlso t[i] >= 97 AndAlso t[i] <= min(122, 87 + b) Then
       digit = t[i] - 87
     End If
     If digit = -1 Then Return 0 '' invalid digit present
     If digit > 0 Then result += multiplier * digit
     multiplier *= b
  Next
  Return result
End Function

Dim s As String

For b As UInteger = 2 To 36
  Print "36 base ";
  Print Using "##"; b; 
  s = ConvertToBase(36, b)
  Print " = "; s; Tab(21); " -> base ";
  Print Using "##"; b; 
  Print " = "; convertToDecimal(s, b)
Next

Print
Print "Press any key to quit"
Sleep

  

You may also check:How to resolve the algorithm Entropy step by step in the Objeck programming language
You may also check:How to resolve the algorithm Perfect numbers step by step in the Sidef programming language
You may also check:How to resolve the algorithm Vogel's approximation method step by step in the Python programming language
You may also check:How to resolve the algorithm Blum integer step by step in the Perl programming language
You may also check:How to resolve the algorithm Bioinformatics/Global alignment step by step in the C++ programming language