Hex2Dec (hex string to decimal)

Share your advanced PureBasic knowledge/code with the community.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Hex2Dec (hex string to decimal)

Post by walbus »

This is a cool little code wilbert
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Hex2Dec (hex string to decimal)

Post by Lunasole »

wilbert wrote: Here's a bit manipulation example to convert two hex characters to a byte value.
Nice, It's about 3.5 times faster than regular Val().
However the absolute result of Purebasic Val() test is 1150ms for 20kk iterations on that my oldest testing CPU (20kk is equivalent of 1 250 000 MD5 hashes translation), that's generally not too slow to optimize it more ^^
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Hex2Dec (hex string to decimal)

Post by walbus »

The better is the enemy of the good
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Hex2Dec (hex string to decimal)

Post by Lunasole »

Updated the code with following changes:

- now it returns output array size
- now it correctly defines array size for hex string of any length (will it be "", or "F", or "FFFFFF"), used some weird construction for that
- removed code of simple unoptimized version (it is anyway too slow and has no advantages against 2nd variant)

Well, I will not even mention one more time how lazy I am, when not doing all the stuff for 100% from the beginning, leaving it instead "as is", until some bug will raise / some functionality will be required ^_^ In most such non-critical stuff I'm just doing only 80% of the work which covers 80% of usage cases. Generally this is bad, but practically ... it is fine and not boring, at least for myself :3
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Al_the_dutch
User
User
Posts: 66
Joined: Mon Nov 11, 2013 11:07 am
Location: Portugal

Re: Hex2Dec (hex string to decimal)

Post by Al_the_dutch »

Perhaps I am doing something wrong here, but if I test it with powers of 2 I get strange results.
Please let me know what I am doing wrong or....
Thx!

Code: Select all

; test/example
Dim Key.a(0)

For i = 0 To Hex2Dec(Key(), "80") ; Hex 80 = 128 and it says 128
   Debug Key(i)
Next i

For i = 0 To Hex2Dec(Key(), "800") ; Hex 800 = 2048 but it says 1280
   Debug Key(i)
Next i

For i = 0 To Hex2Dec(Key(), "8000") ; Hex 8000 = 32768 but it says 1280
   Debug Key(i)
Next i

For i = 0 To Hex2Dec(Key(), "80000") ; Hex 80000 = 524288 but it says 12800
   Debug Key(i)
Next i
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Hex2Dec (hex string to decimal)

Post by Lunasole »

wilbert wrote: Sat Aug 20, 2016 7:39 pm
Lunasole wrote:I liked "optimizations for optimizations"
I still do :lol:
I guess it's a leftover from staring with a computer with a 3.5 Mhz processor.
You are right ASM is more difficult to debug. Even without using ASM, there's still room for optimization but a bit less of course.
Here's a bit manipulation example to convert two hex characters to a byte value.
Man I've seen that already, hah. As well as many posts here I also seeing again when looked to this forum again.
What ***** are trying to go in circles? That only makes things worse, you know that.
In my case I did that sometime previously as I remember, that was caused by conditions surrounded me and other *****s caused that, unintentionally. Now I'd not say the overall conditions are much better, but at least my own are more stable and closer to what I usually was, unlike shitty states when you smoke marijuana and have a noticeable external informational pressure, and so on.
Anyway **** who tried all that on me failed after all and only make that this *** spread more, but I see that matrix repeats here too nowadays.
It would be more wise and so on not to play with that, but say directly^^ But I'm not surprised that those ****** still didn't learned that.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Hex2Dec (hex string to decimal)

Post by Lunasole »

What about my that code of 2016 it maybe looks funny. Should be made better, but I didn't updated it more because was not needed.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
idle
Always Here
Always Here
Posts: 5094
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Hex2Dec (hex string to decimal)

Post by idle »

Something like this might be better.

Code: Select all

Procedure.q HEXTODEC(*hex) 
  Protected *p.Unicode
  Protected out.q 
  
  Static Dim lut(102) 
  If lut(102) <> $F 
    lut(48)=0 
    lut(49)=1 
    lut(50)=2
    lut(51)=3 
    lut(52)=4 
    lut(53)=5
    lut(54)=6 
    lut(55)=7 
    lut(56)=8
    lut(57)=9 
    lut(65)=$A 
    lut(66)=$B 
    lut(67)=$C
    lut(68)=$D
    lut(69)=$E
    lut(70)=$F 
    lut(97)=$A
    lut(98)=$B 
    lut(99)=$C
    lut(100)=$D
    lut(101)=$E
    lut(102)=$F 
  EndIf  
   
  *p=*hex  
  While *p\u    
    out << 4 
    out | lut(*p\u)
    *p+2 
  Wend 
  ProcedureReturn out      
EndProcedure   

Debug Hex(HexToDec(@"F"))		
Debug Hex(HexToDec(@"DEF"))		
Debug Hex(HexToDec(@"01234"))	
Debug Hex(HexToDec(@"9abcd"))	
Debug Hex(HexToDec(@"1FFFFFFFFFFFFFDC"))


jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

Re: Hex2Dec (hex string to decimal)

Post by jassing »

Just curious -- why not use val("$FFFFFFFFFFFFFFFE")?
Hex(val("$FFFFFFFFFFFFFFFE")) = "FFFFFFFFFFFFFFFE"
User avatar
idle
Always Here
Always Here
Posts: 5094
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Hex2Dec (hex string to decimal)

Post by idle »

don't know but my code example will be at least 2 times faster than Val if it's compiled with the the c backend
Al_the_dutch
User
User
Posts: 66
Joined: Mon Nov 11, 2013 11:07 am
Location: Portugal

Re: Hex2Dec (hex string to decimal)

Post by Al_the_dutch »

Thx idle, for hex values fitting in .q variables this works fine.
For bigger Values (using BigInt) the code of Lunasole was promissing. But I understand what Lunasole is saying about old code.
jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

Re: Hex2Dec (hex string to decimal)

Post by jassing »

idle wrote: Tue Apr 25, 2023 2:29 am don't know but my code example will be at least 2 times faster than Val if it's compiled with the the c backend
Ah, faster is better... It wasn't obvious there would be a speed difference - so I appreciate the explanation. thank you.
User avatar
idle
Always Here
Always Here
Posts: 5094
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Hex2Dec (hex string to decimal)

Post by idle »

I've pruned the thread back and will lock the topic for a while
Post Reply