It is currently Mon Jan 20, 2020 9:49 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: convert binary octal hex to decimal
PostPosted: Thu Dec 26, 2019 6:27 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3457
Location: New Zealand
convert binary octal hex to decimal with constant values or strings, with an Oct equivalent of Bin,Hex

Code:
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)




Top
 Profile  
Reply with quote  
 Post subject: Re: convert binary octal hex to decimal
PostPosted: Thu Dec 26, 2019 11:12 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 24, 2012 10:19 am
Posts: 122
I've got an error on Windows XP32b+PB5.71 LTS x86

Quote:
PureBasic.asm[111]:MOV qword[v_a],1010b
error:illegal instruction.


M.


Top
 Profile  
Reply with quote  
 Post subject: Re: convert binary octal hex to decimal
PostPosted: Thu Dec 26, 2019 12:06 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3582
Location: Netherlands
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:
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, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: convert binary octal hex to decimal
PostPosted: Thu Dec 26, 2019 8:02 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3457
Location: New Zealand
Thanks Wilbert that's a much better solution, edited post


Top
 Profile  
Reply with quote  
 Post subject: Re: convert binary octal hex to decimal
PostPosted: Fri Dec 27, 2019 1:04 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4600
Location: Lyon - France
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye