To convert a single digit, something like
Btoi(Bytes("1")) - Int(48)
should work. 48 is the ASCII value of “0”. (See ASCII - Wikipedia)
If you need to convert a multidigit number, then you most likely need to extract each digit one by one (using GetByte
), convert them to an integer as above, and reconstruct the resulting decimal number. With two digits (not tested but gives the idea):
def digitStringToUint(s):
return Btoi(s) - Int(48)
numberAsString = Bytes("42")
numberAsUint = (digitStringToUint(GetBytes(numberAsString, Int(0)))) * Int(10) \
+ (digitStringToUint(GetBytes(numberAsString, Int(1))))
Using TEAL v4, you may even create a loop to handle any number of digits, but it’s not yet available to use in PyTEAL to my knowledge.
Note: I’ve not tested the code above. So it may requires some tweaks.