Android Code Snippet Number base conversions

SubName: toDecimal, fromDecimal, fromDecimal2, toDecimal2, FromTo
Author: Unknown + derez

Description: Convert number to other base

Here are 3 subs that perform base conversion to and from decimal to base less then 10 - returns int, and to larger base - returns string.
Edit: added toDecimal2 to convert base greater then 10 (string) to decimal.
Edit: toDecimal2 improved and FromTo added

B4X:
'Convert integer in any base (2<=b<=10) to decimal integer.
Sub toDecimal( n As Int, base As Int) As Int
  Dim result As Int = 0
  Dim multiplier As Int = 1
  Do While n > 0
      result = result + (n Mod 10) * multiplier
      multiplier = multiplier * base
      n = n / 10
  Loop
  Return result
End Sub

'Convert decimal integer to any base (2<=b<=10) .
Sub fromDecimal(n As Int, base As Int) As Int
  Dim result As Int = 0
  Dim multiplier As Int = 1
  Do While n > 0
      result = result + (n Mod base) * multiplier
      multiplier = multiplier * 10
      n = n / base
  Loop
  Return result
End Sub

'Convert decimal integer to any base up to 20, returns string.
Sub fromDecimal2(n As Int, base As Int) As String
  Dim chars As  String ="0123456789ABCDEFGHIJ"
  Dim result As String = ""
  Do While n > 0
      result = chars.charAt(n Mod base) & result
      n = n / base
  Loop
  Return result
End Sub

'Convert any base up to 20 to decimal integer
Sub toDecimal2( n As String, base As Int) As Int
    n = n.ToUpperCase
   Dim result As Int = 0
   Dim st As String
   Dim chars As  String ="0123456789ABCDEFGHIJ"
   Dim k As Int = n.length - 1
   Dim multiplier As Int = 1
   For i =  k To 0 Step -1
       st = n.CharAt(i)
       result = chars.IndexOf(st) * multiplier  + result
       multiplier = multiplier * base
   Next
   Return result
End Sub

'Convert from one base to another, both must be less or equal to 10.
Sub FromTo(n As Int, frombase As Int, tobase As Int) As Int
   Dim t As Int = todecimal(n,frombase)
   Return fromDecimal(t,tobase)
End Sub

Tags : base conversion
 
Last edited:

derez

Expert
Licensed User
Longtime User
Added toDecimal2 to convert base greater then 10 (string) to decimal.
 

derez

Expert
Licensed User
Longtime User
FromTo added (not big deal, just convert to decimal and back).
 

derez

Expert
Licensed User
Longtime User
Thanks.
I would advise you to modify the names.
As this is not a library, everyone can copy the code and give it whatever name he likes !
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi Derez

I'm trying to use your "toDecimal2" code to convert twelve digit [signed six byte] HEX to decimal. No matter what I do B4A operates as signed four byte hex. For example: FFFF,FFFF converts to -1, for signed six byte HEX I would get 4,294,967,295.
Is the signed 4 byte locked in to B4A or have I missed something simple.

Essence of the code below "Dim IP" emulates the input from keypad.

Any help greatly appreciated.

Regards Roger

B4X:
    Dim IP as String = "FFFFFFFF"
    Dim Result As Double = 0
    Dim InputStrChar As String
    Dim chars As  String = "0123456789ABCDEF"
    Dim StrLoop As Int
    Dim multiplier As Int = 1
    Dim InputStr As String
    InputStr = IP.ToUpperCase
    StrLoop = InputStr.length - 1
   
    If Radix_Flag = 16 AND input_flag = 1 Then
        If IP.Length < 13 Then
            For i =  StrLoop To 0 Step -1
               InputStrChar = InputStr.CharAt(i)
                Result = chars.IndexOf(InputStrChar) * multiplier  + Result
                multiplier = multiplier * Radix_Flag
            Next
            IP = Result
            Msgbox(Result, "RESULT")   'Gives -1
            Msgbox(IP, "IP")  'Gives -1
        Else
             Return False
        End If               
    End If
 

derez

Expert
Licensed User
Longtime User
Many times, just putting the problem in the forum helps to solve it, even without any answer...
Thats a magic that Erel embedded in the forum
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…