Hex2Dec (hex string to decimal)
Re: Hex2Dec (hex string to decimal)
Nice, It's about 3.5 times faster than regular Val().wilbert wrote: Here's a bit manipulation example to convert two hex characters to a byte value.
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"
Re: Hex2Dec (hex string to decimal)
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
- 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"
-
- User
- Posts: 66
- Joined: Mon Nov 11, 2013 11:07 am
- Location: Portugal
Re: Hex2Dec (hex string to decimal)
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!
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)
Man I've seen that already, hah. As well as many posts here I also seeing again when looked to this forum again.wilbert wrote: ↑Sat Aug 20, 2016 7:39 pmI still doLunasole wrote:I liked "optimizations for optimizations"
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.
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"
Re: Hex2Dec (hex string to decimal)
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"
Re: Hex2Dec (hex string to decimal)
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)
Just curious -- why not use val("$FFFFFFFFFFFFFFFE")?
Hex(val("$FFFFFFFFFFFFFFFE")) = "FFFFFFFFFFFFFFFE"
Hex(val("$FFFFFFFFFFFFFFFE")) = "FFFFFFFFFFFFFFFE"
Re: Hex2Dec (hex string to decimal)
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
-
- User
- Posts: 66
- Joined: Mon Nov 11, 2013 11:07 am
- Location: Portugal
Re: Hex2Dec (hex string to decimal)
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.
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)
I've pruned the thread back and will lock the topic for a while