Page 1 of 1
Hex2Dec() »»» Converting hex numbers to decimal system
Posted: Thu Jan 18, 2007 3:43 pm
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.
Posted: Thu Jan 18, 2007 4:53 pm
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
Posted: Thu Jan 18, 2007 5:13 pm
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
Posted: Thu Jan 18, 2007 5:42 pm
by AND51
Thank you for sharing your code!

Re: Hex2Dec() »»» Converting hex numbers to decimal system
Posted: Thu Jan 18, 2007 8:53 pm
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?
Posted: Thu Jan 18, 2007 9:05 pm
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?

Posted: Fri Jan 19, 2007 9:19 am
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.

Posted: Fri Jan 19, 2007 10:16 am
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)
Re: Hex2Dec() »»» Converting hex numbers to decimal system
Posted: Fri Nov 06, 2009 11:47 am
by Azul
Just my simple code
Code: Select all
Procedure hex2dec(hex$)
ProcedureReturn Val("$"+hex$)
EndProcedure
Debug hex2dec("A")
Re: Hex2Dec() »»» Converting hex numbers to decimal system
Posted: Fri Nov 06, 2009 11:57 am
by Kaeru Gaman