# Limits on Btoi() in PyTEAL

**URL:** <https://forum.algorand.co/t/limits-on-btoi-in-pyteal/3769>\
**Category:** General\
**Tags:** docs, teal\
**Created:** [July 14, 2021, 11:57pm UTC](https://forum.algorand.co/t/limits-on-btoi-in-pyteal/3769 "2021-07-14T23:57:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![danger\_dave](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@danger\_dave](https://forum.algorand.co/u/danger_dave)\
**Post date:** [July 14, 2021, 11:57pm UTC](https://forum.algorand.co/t/limits-on-btoi-in-pyteal/3769/1 "2021-07-14T23:57:32Z")

</div>

Hey guys!

I’m trying to understand some of the PyTEAL functions better. According to the [docs](https://pyteal.readthedocs.io/en/latest/data_type.html#conversion), the Btoi() function returns a uint64.

My question is: What would happen if one passed the bytes of a number with more than 21 digits (greater than 2^64) to Btoi? Would the transaction be automatically rejected because of this operation?

Here’s my sample code

```auto
# send_tx.py
# assume params/addresses/amt/key have been declared already

note = '12345678909876543210321'.encode()
PaymentTxn(address1, params, address3, amt, note=note).sign(key)

```

This signed txn is now paired with another txn signed by logic signed by address2. The PyTEAL logic is

```auto
def teal_prog():

    core = And(
        Global.group_size() == Int(2),
        Gtxn[0].type_enum() == TxnType.Payment,
        Gtxn[0].sender() == address1,
        Gtxn[0].receiver() == Gtxn[1].receiver(),
        Gtxn[1].type_enum() == TxnType.Payment,
        Gtxn[1].amount() == Btoi(Gtxn[0].note()) % 100000,
    )
    return core

```

( address1 uses the note field to tell address2 how many algos they owe. For whatever reason, address1 didn’t realize their note field was so long! Luckily, address2 set a limit of 99999 microalgos )

What happens when address1 submits the two PaymentTxns (after signing with the LogicSig)?

---

<div class="post-metadata">

**Author:** ![fabrice](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@fabrice](https://forum.algorand.co/u/fabrice)\
**Post date:** [July 15, 2021, 12:36am UTC](https://forum.algorand.co/t/limits-on-btoi-in-pyteal/3769/2 "2021-07-15T00:36:58Z")

</div>

Your example will not work because `Btoi` does not convert a string representing a number.  
It takes as input a byte array representing a 64-bit number in big endian.  
See [How to convert "1" to 1 in Teal? - #2 by fabrice](https://forum.algorand.co/t/how-to-convert-1-to-1-in-teal/3585/2) and [Endianness - Wikipedia](https://en.wikipedia.org/wiki/Endianness#Example)

---

<div class="post-metadata">

**Author:** ![danger\_dave](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@danger\_dave](https://forum.algorand.co/u/danger_dave)\
**Post date:** [July 15, 2021, 1:08am UTC](https://forum.algorand.co/t/limits-on-btoi-in-pyteal/3769/3 "2021-07-15T01:08:16Z")

</div>

Is there an easier way to pass a number through the “note” field of a transaction? Like is this the best one can do:

```auto
note = '123'.encode()

```

I found another way to pass a note as a message pack, but I’m not sure if that makes TEAL conversion easier

```auto
import base64
from algosdk import encoding

note = base64.b64decode(encoding.msgpack_encode({"num":str(17772)}))

```

---

<div class="post-metadata">

**Author:** ![fabrice](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@fabrice](https://forum.algorand.co/u/fabrice)\
**Post date:** [July 15, 2021, 1:39am UTC](https://forum.algorand.co/t/limits-on-btoi-in-pyteal/3769/4 "2021-07-15T01:39:39Z")

</div>

None of these options can easily be decoded using TEAL.

What you need to do is encode the number in big endian:

```python
note = (123).to_bytes(8,'big')

```

Note that in the future, you may consider following the draft ARC: [ARCs/arc-0002.md at main · algorandfoundation/ARCs · GitHub](https://github.com/algorandfoundation/ARCs/blob/main/ARCs/arc-0002.md) and prefix your note with `myapplication:b`. I.e.:

```python
note = "myapplication:b".encode() + (123).to_bytes(8,'big')

```

Since it’s still a draft, it may change.

---

<div class="post-metadata">

**Author:** ![danger\_dave](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@danger\_dave](https://forum.algorand.co/u/danger_dave)\
**Post date:** [July 15, 2021, 1:55am UTC](https://forum.algorand.co/t/limits-on-btoi-in-pyteal/3769/5 "2021-07-15T01:55:06Z")

</div>

Great work on those conventions! They sound like an excellent idea!

My only worry with following them is that they’ll complicate Btoi(txn.note()) operations.

Bringing this full circle to my original question, what would happen if I set

```auto
note = (123456789098765432100112).to_bytes(8,"big")

```

and then checked that against the PyTEAL logic

```auto
Gtxn[1].amount() == Btoi(Gtxn[0].note()) % 100000

```

(where the note is attached to the first transaction)

---

<div class="post-metadata">

**Author:** ![fabrice](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@fabrice](https://forum.algorand.co/u/fabrice)\
**Post date:** [July 15, 2021, 2:34am UTC](https://forum.algorand.co/t/limits-on-btoi-in-pyteal/3769/6 "2021-07-15T02:34:02Z")

</div>

You can use the `subtring` opcodes to parse the note and remove the prefix.

`btoi` panic if the input is too long:

> **[Opcodes - Algorand Developer Portal](https://developer.algorand.org/docs/reference/teal/opcodes/#btoi)**

This means that the smart contract fails.

---

<div class="post-metadata">

**Author:** ![danger\_dave](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@danger\_dave](https://forum.algorand.co/u/danger_dave)\
**Post date:** [July 15, 2021, 2:05pm UTC](https://forum.algorand.co/t/limits-on-btoi-in-pyteal/3769/7 "2021-07-15T14:05:40Z")

</div>

Thank you so much! That was extremely helpful!
