Skip to content
This new developer portal is under construction. For complete documentation, please refer to the old developer portal.

Encoding and Decoding

Consistent data representation is crucial when interacting with Algorand smart contracts. This section explains the fundamental concepts of encoding and decoding information so that on-chain logic and off-chain applications remain compatible. By following these guidelines, developers can ensure reliable data handling and a seamless flow of information throughout the development lifecycle.

Encoding Types

JSON

The encoding most often returned when querying the state of the chain is JSON.

It is easy to visually inspect but may be relatively slow to parse.

All byte arrays are base 64 encoded strings

MessagePack

The encoding used when transmitting transactions to a node is MessagePack.

To inspect a given msgpack file contents a convenience commandline tool is provided:

Terminal window
1
msgpacktool -d < file.msgp

Base64

The encoding for byte arrays is Base64.

This is to make it safe for the byte array to be transmitted as part of a json object.

Base32

The encoding used for Addresses and Transaction Ids is Base32.

Individual Field Encodings

Address

In Algorand a public key is a 32 byte array.

The Accounts or Addresses are typically shown as a 58 character long string corresponding to a base32 encoding of the byte array of the public key + a checksum.

Given an address 4H5UNRBJ2Q6JENAXQ6HNTGKLKINP4J4VTQBEPK5F3I6RDICMZBPGNH6KD4, encoding to and from the public key format can be done as follows:

1
address = "4H5UNRBJ2Q6JENAXQ6HNTGKLKINP4J4VTQBEPK5F3I6RDICMZBPGNH6KD4"
2
pk = encoding.decode_address(address)
3
addr = encoding.encode_address(pk)
4
5
assert addr == address

Snippet Source

Byte arrays

When transmitting an array of bytes over the network, byte arrays are base64 encoded. The SDK will handle encoding from a byte array to base64 but may not decode some fields and you’ll have to handle it yourself. For example compiled program results or the keys and values in a state delta in an application call will be returned as base64 encoded strings.

Example:

Given a base64 encoded byte array SGksIEknbSBkZWNvZGVkIGZyb20gYmFzZTY0 it may be decoded as follows:

1
base64.b64decode(encoded_str).decode("utf-8") print(decoded_str) ``` [Snippet
2
Source](https://github.com/algorand/js-algorand-sdk/blob/examples/examples/codec.ts#L23-L26)
3
</TabItem>
4
<TabItem label='JavaScript' icon='seti:javascript'>
5
```javascript const b64Encoded = 'SGksIEknbSBkZWNvZGVkIGZyb20gYmFzZTY0'; const b64Decoded =
6
Buffer.from(b64Encoded, 'base64').toString(); console.log(b64Encoded, b64Decoded); ``` [Snippet
7
Source](https://github.com/algorand/py-algorand-sdk/blob/examples/examples/codec.py#L24-L27)
8
</TabItem>
9
<TabItem label='Go' icon='seti:go'>
10
```go encoded := "SGksIEknbSBkZWNvZGVkIGZyb20gYmFzZTY0" decoded, _ :=
11
base64.StdEncoding.DecodeString(encoded) reencoded := base64.StdEncoding.EncodeToString(decoded)
12
``` [Snippet
13
Source](https://github.com/algorand/go-algorand-sdk/blob/examples/examples/codec/main.go#L84-L87)
14
</TabItem>
15
<TabItem label='Java' icon='seti:java'>
16
```java String encodedStr = "SGksIEknbSBkZWNvZGVkIGZyb20gYmFzZTY0"; byte[] decodedBytes =
17
Encoder.decodeFromBase64(encodedStr); String reEncodedStr =
18
Encoder.encodeToBase64(decodedBytes); assert encodedStr.equals(reEncodedStr); ``` [Snippet
19
Source](https://github.com/algorand/java-algorand-sdk/blob/examples/examples/src/main/java/com/algorand/examples/CodecExamples.java#L35-L39)
20
</TabItem>
21
</Tabs>
22
23
### Integers
24
25
Integers in algorand are almost always uint64, sometimes it's required to encode them as bytes. For example when passing them as application arguments in an ApplicationCallTransaction. When encoding an integer to pass as an application argument, the integer should be encoded as the big endian 8 byte representation of the integer value.
26
27
_Example:_
28
29
Given an integer `1337`, you may encode it as:
30
31
<Tabs syncKey="lang">
32
<TabItem label="Python" icon="seti:python">
33
```python
34
val = 1337
35
encoded_uint = val.to_bytes(8, "big")
36
decoded_uint = int.from_bytes(encoded_uint, byteorder="big")
37
assert decoded_uint == val

Snippet Source

Working with Encoded Structures

transactions

Sometimes an application needs to transmit a transaction or transaction group between the front end and back end. This can be done by msgpack encoding the transaction object on one side and msgpack decoding it on the other side. Often the msgpack’d bytes will be base64 encoded so that they can be safely transmitted in some json payload so we use that encoding here.

Essentially the encoding is:

tx_byte_str = base64encode(msgpack_encode(tx_obj))

and decoding is:

tx_obj = msgpack_decode(base64decode(tx_byte_str))

Example:

Create a payment transaction from one account to another using suggested parameters and amount 10000, we write the msgpack encoded bytes