Conversion of Decimal Integer To ANY BASE.

Share your advanced PureBasic knowledge/code with the community.
User avatar
Machupichu
New User
New User
Posts: 3
Joined: Sun Mar 08, 2020 12:23 am

Conversion of Decimal Integer To ANY BASE.

Post by Machupichu »

:D Hello!!
This is my First Post.

Code: Select all

;{ Conversion of  Decimal Integer To ANY BASE.
Procedure.s DecToAny(Num.l , Base.l = 2, DLM.s=".") 
;-- Declaramos una variable que acumulará la conversión. 
Define iExpr.s, l;, DLM.s= GetStr(*Delim)
;If DLM ="" Then DLM="." :EndIf
If Num >=Base
;-- Declaramos variables. 
Define DVD.l , DVS.l , j.l , e.l , Resto.l   
;-- Le damos un valor inicial al dividendo (DVD). 
DVD = Num  
;-- Hallamos el máximo de divisiones comunes y lo transpasamos a la variable. 
;-- Como vemos eso se consigue hallando la parte entera del Logarithm (en este caso Divendo) en Base (en este caso Divisor). 
e = Int(Log(DVD) / Log(Base)) ; MATH TRICK.
;-- Corro una bucle en función de la largura encontrada. 
For j = 1 To e  
  ;-- Encuentro el resto (residuo) que se generará al dividir DVD con la Base. 
     Resto = Int(Mod(DVD, Base))  
  ;-- Extraemos el cociente (matemáticamente siempre es entero) y revaloramos las variables. 
  DVD = Int(DVD/Base)  
  ;-- Actualizamos el DVS (Divisor) con el valor el Dividendo. 
  DVS = DVD  
  ;-- Concatenamos como caracter eneario que lo obtenemos del resto, y finalmente cierro el loop. 
  iExpr = DLM + Str(Resto) + iExpr
Next 
;-- Concateno el último caracter, pero esta ves con el divisor DVS. 
iExpr = Str(DVS) + iExpr  
EndIf
ProcedureReturn iExpr  
EndProcedure
Procedure.l AnyToDec(iExpr.s, Base.l = 2, DLM.s=".") 
;-- Declaramos variables
Define e.l, DVD.l , DVS.l , j.l , Resto.l;, iExpr.s= GetStr(*Delim),  DLM.s= GetStr(*Delim)
;-- Protegemos.
;If DLM ="" Then DLM="." :EndIf
;-- Debe haber como mínimo un delimitador, de lo contrario finalizamos. 
If FindString(iExpr, DLM) 
;-- Inicializamos valores
DVS= (-1) : Base= Abs(Base)
;-- Hallo la cantidad de Bloques.
e = CountString(iExpr, DLM) + 1
;-- Revaloro a cero. 
DVS= 0
;-- Le damos un valor inicial al divisor (DVD), tal será el primer bloque o campo. 
DVS =Val(StringField(iExpr, 1, DLM))
;-- Corro una bucle en función de la cantidad de campos (Bloques), pero no consideraré el 1ro. 
For j = 2 To e 
;-- Identifico al Resto, este en primera instancia sería el campo respectivo. 
 Resto = Val(StringField(iExpr, j, DLM))
;-- Hallo el Dividendo multiplicando el Divisor por la Base y sumando el Resto. 
   DVD = DVS * Base + Resto  
;-- Actualizamos el DVS (Divisor) con el valor el Dividendo. 
   DVS = DVD  
 Next  
EndIf
;-- Transpasamos el divisor al retorno de la función. 
ProcedureReturn DVS   
EndProcedure
;}

;EXAMPLE
CompilerIf #PB_Compiler_IsMainFile
; To Base '5'
Debug DecToAny(1567718976, 5,"."); 1.1.2.0.2.3.1.4.0.0.1.4.0.1
; To Base '2'
Debug DecToAny(1567718976, 2,"."); 1.0.1.1.1.0.1.0.1.1.1.0.0.0.1.0.1.1.1.1.1.1.0.0.1.0.0.0.0.0.0
; To Base '7'
Debug DecToAny(1567718976, 7,"."); 5.3.5.6.4.2.5.1.1.5.5
;==============
; To Decimal (5) 
Debug AnyToDec("1.1.2.0.2.3.1.4.0.0.1.4.0.1", 5, "."); 1567718976
; To Decimal (2)
Debug AnyToDec("1.0.1.1.1.0.1.0.1.1.1.0.0.0.1.0.1.1.1.1.1.1.0.0.1.0.0.0.0.0.0", 2, "."); 1567718976
; To Decimal (7)
Debug AnyToDec("5.3.5.6.4.2.5.1.1.5.5", 7, "."); 1567718976
CompilerEndIf
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Conversion of Decimal Integer To ANY BASE.

Post by Little John »

Radix conversion has been done before, see e.g. viewtopic.php?f=12&t=55799

BTW, your program is hard to read:
Indentation is inconsistent (in the PureBasic IDE, press Ctrl+A and then Ctrl+I).
Also the actual code is buried in a bunch of comments -- that I (and many others here) cannot even understand, because they are not in English.
User avatar
NicTheQuick
Addict
Addict
Posts: 1523
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Conversion of Decimal Integer To ANY BASE.

Post by NicTheQuick »

I also did something similar years ago for arbitrary long numbers: https://www.purebasic.fr/german/viewtop ... =8&t=14171
You can define the input base, the output base and the number of decimals in case you are running into a repetend.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Conversion of Decimal Integer To ANY BASE.

Post by IdeasVacuum »

Welcome to PB Machupichu, and thank you for sharing your code.

@ NicTheQuick freakscorner is down?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
NicTheQuick
Addict
Addict
Posts: 1523
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Conversion of Decimal Integer To ANY BASE.

Post by NicTheQuick »

IdeasVacuum wrote:Welcome to PB Machupichu, and thank you for sharing your code.

@ NicTheQuick freakscorner is down?
Yes, there are only old information on that page and after the GDPR took effect I was not in the mood to make the page ready for that. So it's gone. :wink:
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Conversion of Decimal Integer To ANY BASE.

Post by IdeasVacuum »

:cry:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply