[Implemented] Val function to work with hexadecimal strings

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
AngelSoul
User
User
Posts: 55
Joined: Tue Jul 29, 2003 9:16 am
Location: Canada

[Implemented] Val function to work with hexadecimal strings

Post by AngelSoul »

Example:
a$ = "$10"
debug Val(a$)

;would be cool if it returns 16 instead of 0
AMD 1.8 - 512mb - ATI All-In-Wonder Radeon 9000 pro - W2k Pro
BASIC programmers never die, they just return without gosub.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Val function to work with hexadecimal in strings

Post by PB »

In the meantime, try this:

Code: Select all

Procedure hex2dec(h$)
  h$=Trim(UCase(h$)) : If Left(h$,1)="$" : h$=Mid(h$,2,999) : EndIf
  For r=1 To Len(h$)
    d<<4 : a$=Mid(h$,r,1)
    If Asc(a$)>60
      d+Asc(a$)-55
    Else
      d+Asc(a$)-48
    EndIf
  Next
  ProcedureReturn d
EndProcedure

a$="$10"
Debug hex2dec(a$)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Val function to work with hexadecimal in strings

Post by PB »

@Fred: Please make a native hex2dec command soon. Please. :)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

I can understand that the native Val() only supports decimal.
it's for all the performance reasons we love PureBasic for...
same like why we have not only Str() but also StrF() and StrQ() and StrD()...

but I agree, a native ValHex(Hex$) would be fine.
oh... and have a nice day.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post by #NULL »

i would make use of a native one too.
sometimes i even used a..
val.l = valBin(binStr.s)
..but mostly for debugging or testing stuff.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post by nco2k »

yes fred, please add a Hex2Dec() soon, its been a looooong time now. :cry:

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Thalius
Enthusiast
Enthusiast
Posts: 711
Joined: Thu Jul 17, 2003 4:15 pm
Contact:

Post by Thalius »

"In 3D there is never enough Time to do Things right,
but there's always enough Time to make them *look* right."
"psssst! i steal signatures... don't tell anyone! ;)"
Post Reply