How to convert "1" to 1 in Teal?

How can I convert Bytes("1") to Int(1) using PyTeal? Assert(Btoi(Bytes("1")) == Int(1)) gives logic eval error: assert failed. That’s consistent with the warning “operations are not meant to convert between human-readable strings and numbers” from here: Data Types and Constants — PyTeal documentation.

So, if my URL is just a number (saved as Bytes) how can use it in Teal arithmetic?

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.