Ok, lets say I have some app args
Gtxn[2].application_args[0]
And they contain
Bytes("134567890")
How can I convert Bytes("134567890")
to Int(1234567890)
in Pyteal? Thanks
Ok, lets say I have some app args
Gtxn[2].application_args[0]
And they contain
Bytes("134567890")
How can I convert Bytes("134567890")
to Int(1234567890)
in Pyteal? Thanks
You need to write a subroutine for that.
You can look at:
That being said, in the specific case where the integer is an application argument, I would strongly recommend you to instead encode the integer following:
Store an int in key local variable - #2 by fabrice (in smart contract)
Save price/amount in contract - sdk js - #2 by fabrice (for encoding in the SDK)
This is much more compact and much less error-prone.
Decimal strings representing integers (like “123456”) should only be used for human-readable values (e.g., in Log
) or in cases where you really have no choice of encoding (e.g., if you get a signed decimal integer).
Alright, thanks. Is the following code correct? I found it on github
_ascii_zero = 48
_ascii_nine = _ascii_zero + 9
ascii_zero = Int(_ascii_zero)
ascii_nine = Int(_ascii_nine)
@Subroutine(TealType.uint64)
def ascii_to_int(arg):
"""ascii_to_int converts the integer representing a character in ascii to the actual integer it represents
Args:
arg: uint64 in the range 48-57 that is to be converted to an integer
Returns:
uint64 that is the value the ascii character passed in represents
"""
return Seq(Assert(arg >= ascii_zero), Assert(arg <= ascii_nine), arg - ascii_zero)
Call function:
int = ascii_to_int(Btoi(Txn.application_args[0]))
No, ascii_to_int
is for a single character.
You need atoi
: pyteal-utils/string.py at main · algorand/pyteal-utils · GitHub
that itself uses ascii_to_int
.
Thank you for helping me!