How to I split Int in Teal

I want to convert int to byte using setbyte. So planning to do with subroutine, but I need to split the integer with individual digits. For example:

  • int 21
  • split: 2 and 1
  • convert 2 to “2”
  • convert 1 to “1”
  • concat “2” and “1”

How I can split int 21?

Thanks!

In the AVM ints are uint64 or with Itob(val) 8 bytes big endian. The digits you posted are the ascii representation of the number as a string.

You can convert the int to ascii chars with something like pyteal-utils/string.py at main · algorand/pyteal-utils · GitHub

But I suspect that is not what you actually want to do. What is the true end goal?

Thanks for the answer!

The end goal is to loop with a given X uint64 and for each loop var concatenate it with bytes, in order to construct known key. For instance key_1, …, key_24, … So i need that “24” in bytes to concat with “key_”. And I’m doing in TEAL.

Basically I need to split those uint64 with two digits. The rest can be done with setbyte by converting it to ascii.

I see, if there is not a very strong reason to make it human readable i’d favor just using the bytes of the int.

In other words, for the integer idx, Suffix(idx, Int(6)) for up to max uint16 unique keys. This would be much more efficient than using ascii and would still provide the unique keys. Any client would have to know that you’re just appending the uint16 bytes but seems fine unless you have a very strong reason not to

1 Like

Ok. Would love to make it with human readable, but there is not much need of it. Sounds right using bytes of the int. Thanks Ben!

1 Like