Hex2Dec() »»» Converting hex numbers to decimal system

Share your advanced PureBasic knowledge/code with the community.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Hex2Dec() »»» Converting hex numbers to decimal system

Post by AND51 »

Hello!

I've just finished my Hex2Dec() function.
  • My function is case-in-sensitive, you can give it any string you want. It examines the hex-string from behind and stops, when detecting an invalid character. So you can even give my function strings like "$123" or "0xFF". It's able to work with very high hex-numbers which are returned by HexQ().
Here's the code:

Code: Select all

Procedure.q Hex2Dec(Hex.s)
	Protected result.q, n, temp, pow.q=1
	hex=UCase(hex)
	For n=MemoryStringLength(@Hex)-1 To 0 Step -1
		temp=PeekC(@Hex+n*SizeOf(Character))-48
		If temp >= 17 And temp <= 22
			temp-7
		ElseIf temp < 0 Or temp > 9
			Break
		EndIf
		result+temp*pow
		pow*16
	Next
	ProcedureReturn result
EndProcedure


Debug $Affe123
Debug Hex2Dec("$Affe123")
Any other solutions/procedures are welcome! So if you want to start a kind of competition... Feel free to post any comments.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

This is longer and not as bullet-proof, but it's also about twice as fast:

Code: Select all

Procedure.q Hex2Dec2(Hex.s)
  Protected Result.q
  Protected Char.l
  Ptr = @Hex
  Char = PeekC(Ptr) - '0'
  If Char = '$' - '0'
    Ptr + SizeOf(Character)
  EndIf
  If Char = '0' - '0'
    Ptr + SizeOf(Character)
  EndIf
  If Char = 'x' - '0'
    Ptr + SizeOf(Character)
  EndIf
  While Char <> -'0'
    Ptr + SizeOf(Character)
    If Char >= ('A' - '0')
      If Char >= ('a' - '0')
        Char - ('a' - $A - '0')
      Else
        Char - ('A' - $A - '0')
      EndIf
    EndIf
    Result = Result << 4 + Char
    Char = PeekC(Ptr) - '0'
  Wend
  ProcedureReturn Result
EndProcedure
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

universal converter, dunno if i already posted it before, 3.94 though!

Code: Select all

Procedure.l x_val(string.s)                                          ; string to val, recognizes hex dec bin oct
  Global x_val_type.l
  Protected p.l, l.l, b.l, t.l, c.l, s.l, m.l, p0.l
  ;
  ; *** as normal val() except it accepts also &H &O &0 &B % \ $ 0X
  ;
  string = UCase(Trim(string))
  l = Len(string)
  p = @string
  p0 = p
  s = PeekB(p) & $FF
  t = 0
  ;
  If s = '-'
    m = -1
    p = p+1
    s = PeekB(p) & $FF
  Else
    m = 1
    If s = '+'
      p = p+1
      s = PeekB(p) & $FF
    EndIf
  EndIf
  ;
  If s = '$'
    p = p+1
    b = 16
  ElseIf s = '%'
    p = p+1
    b = 2
  ElseIf s = '\'
    p = p+1
    b = 8
  ElseIf Left(string,2) = "&B"
    p = p+2
    b = 2
  ElseIf Left(string,2) = "&O"
    p = p+2
    b = 8
  ElseIf Left(string,2) = "&0"
    p = p+2
    b = 8
  ElseIf Left(string,2) = "&H"
    p = p+2
    b = 16
  ElseIf Left(string,2) = "0X"
    p = p+2
    b = 16
    ;
    ; ElseIf Left(string,1) = "0"           ; i don't like this one, as i often use
    ;    p = p+1                            ; preceding zeroes in front of decimals while
    ;    b = 8                              ; c(++) would turn those into octals... brrr...
    ;                                       ; well, it's up to you to uncomment these lines
  Else
    b = 10
  EndIf
  ;
  While p < p0+l
    c = (PeekB(p) & $FF) - 48
    p = p+1
    If c > 9
      c = c - 7
    EndIf
    If c >= 0 And c < b
      t = t*b+c
    Else
      p = p+l
    EndIf
  Wend
  t = t * m
  x_val_type = b
  ;
  ProcedureReturn t
EndProcedure
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Thank you for sharing your code! :D
PB 4.30

Code: Select all

onErrorGoto(?Fred)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Hex2Dec() »»» Converting hex numbers to decimal system

Post by PB »

Here's mine from 2003: http://www.purebasic.fr/english/viewtopic.php?t=7647

@Fred/Freak: Can we really have this as a native command one day, please?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Why one day?
I don't want to be cheeky, but we already delivered the code. Somebody must simply optimized the code and put it into ASM, so that it can be included into PureBasic.

You already wrote that procedure in 2003? Well, what happened in the last 4 years, that this command has not been included yet? :?
PB 4.30

Code: Select all

onErrorGoto(?Fred)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Why one day?

Because there is a native command called Hex() which converts a decimal
value to hex, so it's only natural that an opposite command should exist to
convert the other way, instead of having to write procedures or use libs.
It's a command that is very important and used quite often, and it's very
annoying to have to paste a procedure in every time I start a new app.

> what happened in the last 4 years, that this command has not been
> included yet? :?

Exactly what I would like to know. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Or a Base command to convert from any base to any base...

eg:

string1$=Base(string2$,base2[,base1=10])

Code: Select all

Procedure.s Base(string$,base,base2=10)
	Static table$="0123456789abcdef"
	If base>1 And base<17 And base2>1 And base2<17
		If base=base2
			result$=string$
		Else
			If base>10
				string$=LCase(string$)
			EndIf
			For loop=1 To Len(string$)
				digit=FindString(table$,Mid(string$,loop,1),1)
				If digit
					number.q*base
					number+(digit-1)
				EndIf
			Next
			If base2=10
				result$=StrQ(number)
			Else
				Repeat
					remainder=number%base2
					number=number/base2
					result$=Mid(table$,remainder+1,1)+result$
				Until number=0
			EndIf
		EndIf
	EndIf
	ProcedureReturn result$
EndProcedure

Debug Base("A",16)
Debug Base("1010",2)
Debug Base("0101",2)
Debug Base("F",16)
Debug Base("10",10,16)
Debug Base("1010",2,16)
Debug Base("0101",2,16)
Debug Base("256",10,16)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
Azul
Enthusiast
Enthusiast
Posts: 109
Joined: Fri Dec 29, 2006 9:50 pm
Location: Finland

Re: Hex2Dec() »»» Converting hex numbers to decimal system

Post by Azul »

Just my simple code

Code: Select all

Procedure hex2dec(hex$)
ProcedureReturn Val("$"+hex$)
EndProcedure

Debug hex2dec("A")

Code: Select all

; Hello, World!
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Hex2Dec() »»» Converting hex numbers to decimal system

Post by Kaeru Gaman »

oh... and have a nice day.
Post Reply