Description

Given a hexadecimal string, return its decimal value.

1
'1e240' ⟹ 123456

My solution

f1  define('hex2dec(hex)', 't') :(bgn)

t   t = table()
    t<'a'> = 10
    t<'b'> = 11
    t<'c'> = 12
    t<'d'> = 13
    t<'e'> = 14
    t<'f'> = 15

;   Size of hex input, to be used as exponent.
    sz = size(hex)

;   Result of the conversion.
    res = 0


;   ## Loop performing the conversion

;   Assign one character of `hex` to `ch`.
lp  hex arb len(1) . ch = '' :f(hex2dec)

;   Convert `ch`, representing one hex character, to decimal:
;   if it's an alphabetic character, look it up in the table;
;   multiply its decimal value by 16 to the power of its position
;   in the hex string, beginning with `sz` - 1 and decrementing
;   from left to right.
    '0123456789' ch :s(cmp)
az  ch = t<ch>
cmp dec = ch * (16 ** (sz - 1))

;   Decrement the current exponent and update the current
;   result of the conversion, then jump to label `lp`.
    sz = sz - 1
    res = res + dec :(lp)


;   ## Result

;   When no characters remain in `hex`, we jump here from
;   label `lp` and return the current value of `res`.
hex2dec hex2dec = res :(return)



;   ## Test

bgn
test   res = hex2dec('1e240')
       ident(res, 123456) :s(end)
err    output = 'Error; expected 123456, actual: ' res
end