Page 2 of 2
Re: Hex2Dec (hex string to decimal)
Posted: Sun Aug 21, 2016 11:46 am
by walbus
This is a cool little code wilbert
Re: Hex2Dec (hex string to decimal)
Posted: Sun Aug 21, 2016 3:21 pm
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 ^^
Re: Hex2Dec (hex string to decimal)
Posted: Sun Aug 21, 2016 3:29 pm
by walbus
The better is the enemy of the good
Re: Hex2Dec (hex string to decimal)
Posted: Sat Feb 18, 2017 3:13 am
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
Re: Hex2Dec (hex string to decimal)
Posted: Mon Apr 24, 2023 11:10 pm
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
Re: Hex2Dec (hex string to decimal)
Posted: Mon Apr 24, 2023 11:43 pm
by Lunasole
wilbert wrote: Sat Aug 20, 2016 7:39 pm
Lunasole wrote:I liked "optimizations for optimizations"
I still do
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.
Re: Hex2Dec (hex string to decimal)
Posted: Mon Apr 24, 2023 11:53 pm
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.
Re: Hex2Dec (hex string to decimal)
Posted: Tue Apr 25, 2023 12:12 am
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"))
Re: Hex2Dec (hex string to decimal)
Posted: Tue Apr 25, 2023 1:31 am
by jassing
Just curious -- why not use val("$FFFFFFFFFFFFFFFE")?
Hex(val("$FFFFFFFFFFFFFFFE")) = "FFFFFFFFFFFFFFFE"
Re: Hex2Dec (hex string to decimal)
Posted: Tue Apr 25, 2023 2:29 am
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
Re: Hex2Dec (hex string to decimal)
Posted: Tue Apr 25, 2023 1:08 pm
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.
Re: Hex2Dec (hex string to decimal)
Posted: Tue Apr 25, 2023 2:20 pm
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.
Re: Hex2Dec (hex string to decimal)
Posted: Wed Apr 26, 2023 10:26 pm
by idle
I've pruned the thread back and will lock the topic for a while