Task

Take a   Natural Number   in a given base and return the sum of its digits:

   110         sums to   1
   123410   sums to   10
   fe16       sums to   29
   f0e16     sums to   29


FUNCTION sumDigits(num AS STRING, bas AS LONG) AS LONG
    'can handle up to base 36
    DIM outp AS LONG
    DIM validNums AS STRING, tmp AS LONG, x AS LONG, lennum AS LONG, L0 AS LONG
    'ensure num contains only valid characters
    validNums = LEFT$("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", bas)
    lennum = LEN(num)
    FOR L0 = lennum TO 1 STEP -1
        x = INSTR(validNums, UCASE$(MID$(num, L0, 1))) - 1
        IF -1 = x THEN EXIT FUNCTION
        tmp = tmp + (x * (bas ^ (lennum - L0)))
    NEXT
    WHILE tmp
        outp = outp + (tmp MOD bas)
        tmp = tmp \ bas
    WEND
    sumDigits = outp
END FUNCTION
 
PRINT sumDigits(LTRIM$(STR$(1)), 10)
PRINT sumDigits(LTRIM$(STR$(1234)), 10)
PRINT sumDigits(LTRIM$(STR$(&HFE)), 16)
PRINT sumDigits(LTRIM$(STR$(&HF0E)), 16)
PRINT sumDigits("2", 2)