It takes hex string of any len and turns it into a byte array.
So generally, it is not for something like "convert hex to a long". I've made it to convert hash-strings (very long and not fitting in any decimal type, also can have variable size) to their raw form.
Also had an idea to convert into array of integers/quads and so on, but that's more complicated to do.
Here are 2 variants, both of them should work fine in Unicode and ASCII modes.
Short and clear version:
Code: Select all
// unactual
Optimized version. For even more speed - use assembler code by @wilbert or something like it ^^
Code: Select all
; convert hex string into raw bytes [optimized]
; Out() unsigned char array to receive result
; Hex$ string with hex data
; RETURN: decimal valuee are placed to Out() array, size of array returned
Procedure Hex2Dec (Array Out.a (1), Hex$)
Protected t$ = "$ "
Protected *c.Character = @Hex$
Protected pg, p = 1
Protected out_len = Len(Hex$) : out_len + out_len % 2 : out_len * 0.5 - Bool(out_len)
ReDim Out(out_len)
While *c\c
If p > 2
Out(pg) = Val(t$)
PokeC(@t$ + SizeOf(Character), 0)
PokeC(@t$ + SizeOf(Character) * 2, 0)
p = 1
pg + 1
EndIf
PokeC(@t$ + p * SizeOf(Character), *c\c)
p + 1
*c + SizeOf(Character)
Wend
Out(pg) = Val(t$)
ProcedureReturn ArraySize(Out())
EndProcedure
; test/example
Dim Key.a(0)
Debug Hex2Dec(Key(), "") ; 0
Debug Hex2Dec(Key(), "F") ; 0
Debug Hex2Dec(Key(), "FFF") ; 1
Debug Hex2Dec(Key(), "FFFF") ; 1
Debug Hex2Dec(Key(), "FFFFF") ; 2
Debug Hex2Dec(Key(), "FFFFFF") ; 2