convert binary octal hex to decimal

Share your advanced PureBasic knowledge/code with the community.
User avatar
idle
Addict
Addict
Posts: 3632
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

convert binary octal hex to decimal

Post by idle »

convert binary octal hex to decimal with constant values or strings, with an Oct equivalent of Bin,Hex

Code: Select all

Procedure.s Oct(number.q) 
  Protected s.s=Space(8*SizeOf(Character)) 
  For a = 7 To 0 Step -1
    PokeS(@s+a*SizeOf(Character),Str(number & 7),SizeOf(Character),#PB_String_NoZero) 
    number >> 3 
  Next   
  ProcedureReturn Trim(s,"0") 
EndProcedure   

;converts and assigns a variable with a constant in the given base 2,8,16
Macro _DEC(var,val,base) 
  EnableASM 
  CompilerSelect base 
    CompilerCase 2 
      mov var, val#b 
    CompilerCase 8
      mov var, val#o 
    CompilerCase 16
      mov var, 0#val#h    
  CompilerEndSelect 
  DisableASM    
EndMacro 

;take a string from respective bases 16,8,2 convert to decimal  

Procedure.q ConvertToDecimal(*n.Character, base)
  Protected d.u, r.q
  Repeat
    d = *n\c - 48
    If d > 9
      d | 32 : If d > 48 : d - 39 : EndIf
    EndIf
    If d >= base : Break : EndIf
    r = (r * base) + d
    *n + SizeOf(Character)
  ForEver
  ProcedureReturn r
EndProcedure


Define a.i

 _DEC(a,10101,2)   
 Debug "Dec " + Str(a) + " Bin " + Bin(a) 
 _DEC(a,644,8)
 Debug "Dec " + Str(a) + " Oct " + Oct(a) 
 _DEC(a,ABC123,16)
 Debug "Dec " + Str(a) + " Hex " + Hex(a) 

Debug ConvertToDecimal(@"ABC123",16) 
Debug ConvertToDecimal(@"644",8)
Debug ConvertToDecimal(@"10101",2) 


Mesa
Enthusiast
Enthusiast
Posts: 226
Joined: Fri Feb 24, 2012 10:19 am

Re: convert binary octal hex to decimal

Post by Mesa »

I've got an error on Windows XP32b+PB5.71 LTS x86
PureBasic.asm[111]:MOV qword[v_a],1010b
error:illegal instruction.
M.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3734
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: convert binary octal hex to decimal

Post by wilbert »

Mesa wrote:I've got an error on Windows XP32b+PB5.71 LTS x86
If you define a as .i instead of .q it should work.

Here's also a to decimal conversion without the need of Pow.

Code: Select all

Procedure.q ConvertToDecimal(*n.Character, base) 
  Protected d.u, r.q
  Repeat
    d = *n\c - 48
    If d > 9
      d | 32 : If d > 48 : d - 39 : EndIf
    EndIf
    If d >= base : Break : EndIf
    r = (r * base) + d
    *n + SizeOf(Character)
  ForEver
  ProcedureReturn r 
EndProcedure 

Debug ConvertToDecimal(@"abc123", 16) 
Debug ConvertToDecimal(@"644", 8)
Debug ConvertToDecimal(@"10101", 2) 
macOS 10.15 Catalina, Windows 10
User avatar
idle
Addict
Addict
Posts: 3632
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: convert binary octal hex to decimal

Post by idle »

Thanks Wilbert that's a much better solution, edited post
User avatar
Kwai chang caine
Addict
Addict
Posts: 4916
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: convert binary octal hex to decimal

Post by Kwai chang caine »

The code for convertion are always interesting 8) (even if in don't understand all) :oops:
This two codes works, thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply