Solidity Encode and Decode (Easy)

Solidity Programming Language
3 min readMay 31, 2023
Solidity Encode and Decode

This article provides insights into encoding/decoding rules and demonstrates how to use Solidity to encode and decode data that needs to be passed as parameters in a function.

Solidity Encode and Decode

A smart contract consists of variables that store information and functions that perform actions. Basically, some functions are private and can only be accessed from within the contract, while others are public and can be accessed from outside the contract. (Others are external and internal)

This means that applications and individuals can send data to the contract and retrieve data from it. In order to send data to the contract, it needs to be formatted in a way that the contract can understand.

This involves encoding the data according to the rules set by the Ethereum Virtual Machine (EVM). The EVM determines how the encoding process should be carried out.

In Solidity, abi.encode() is utilized for the serialization of data structures. Serialization involves the transformation of a data structure, such as transactions, blocks, smart contract data, or objects, into a sequential byte sequence. This byte sequence is then suitable for storage or transmission across a network. Conversely, abi.decode() is employed for the deserialization process. Deserialization is the reverse procedure, where a given sequence of bytes is taken, and the original data structure or object is reconstructed from it. (Coinsbench)

In Solidity, there exists a global variable, is named “abi”, which includes an encoding/decoding method. This enables us to utilize it for encoding/decoding the parameters of any function.

Encoding in Solidity

We have an string and uint variables and we try to encode these variables.

This is our magical code!

 function encodeData (string memory text, uint256 number) public pure returns(bytes memory b){
return b = abi.encode (text, number);

We take these variables as a parameter in encodeData function and then function returns these variables as bytes thanks to abi.encode.

In Solidity, the memory keyword is used to declare variables that are temporary and only exist during the execution of a function. These variables are not stored on the blockchain but are instead used for intermediate storage and manipulation of data within a function.

Deploying our contract, and calling the function encodeData(…) with the following values for string and unsigned integer: (hello, 25), we get the result.

We can see the result in the remix. This is the result.

Decoding in Solidity

We try to use solidity to decode the parameters of a function. abi.decode is a Solidity function that is used to decode data that has been encoded using the Application Binary Interface (ABI) encoding.

 function decodeData( bytes memory _data) public pure returns (string memory x, uint256 y) {
(x,y) = abi.decode(_data, (string, uint256));

We take this result as a parameter in decodeData function and then function returns our variables (string and uint) thanks to abi.decode.

Thank you for listening us !

Engin YILMAZ (VeriDelisi)



Solidity Programming Language

Solidity basics for beginners: Learn the fundamentals of smart contract development and build your first DApp! #Solidity #Foundry #Ethereum #Opcodes #DApps