Here is another implementation, it is ~3 times faster than the one posted above:
function HexToInteger3(hex_in)
bArr = createobject("roByteArray")
if len(hex_in) mod 2 > 0
'fix for fromHexString() malicious silent failure on odd length
hex_in = "0" + hex_in
end if
bArr.fromHexString(hex_in)
out = 0
for i = 0 to bArr.count()-1
out = 256 * out + bArr[i]
end for
return out
end function
Testing was done with 8-digit hexadecimals that would be typical use (like RGBA), highest number of calls in a second in repeat runs. The new version on 3100 nets at 72 microseconds per call vs the one given at 209 usec. On Roku3 is 4x (13us vs 52us). This is substantial so i suggest somebody from RokuCo replacing the
HexToInteger implementation in
bslCore.brs with this one.
Odds and ends: in our new series "turning lemons into lemonade", watch how because of a quirk in
roByteArray.fromHexString implementation the new function can parse hex-formats that the "old" one cannot:
BrightScript Debugger> ? hexToInteger3(" fff") 'leading spaces
4095
BrightScript Debugger> ? hexToInteger3("$fff") 'Turbo Pascal
4095
BrightScript Debugger> ? hexToInteger3("0xFFF") 'Unix/C
4095
BrightScript Debugger> ? hexToInteger3("&hFFF") 'Microsoft BASIC
4095
BrightScript Debugger> ? hexToInteger3("#0000FF") 'HTML blue
255
BrightScript Debugger> ? hexToInteger3("U+20AC") 'Unicode €
8364