Solidity Conversions

In this article, we will explore the different types of conversions that are available in Solidity and how they can be used.

In Solidity, both explicit and implicit conversions are supported.

In the Solidity compiler, implicit conversion is allowed between two data types as long as it is not possible to perform implicit conversions and no information is lost.

It can be seen that uint8 can be converted to uint16, but int8 can be converted to uint256, as int8 can contain negative values, which are not allowed in uint256.

This article discusses the following conversions:

  1. Implicit Conversions.
  2. Explicit Conversions.
  3. Conversions between Literals and Elementary Types.


Solidity Conversions Implicit

They occur automatically when variables of one type are assigned to variables of another type.

For instance, it is possible to implicitly convert an integer into a fixed point number or a string.

A built-in function such as uint() or int() performs these operations.

Solidity provides the following syntax for some of the most common conversion function:

 int() to uint():

The given code converts the integer to an unsigned integer():

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0; contract SolidityTest{function int_To_uint(int mrx) public pure returns(uint){uint ample=uint(mrx);return ample;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
Note: It is not considered as a good practice to convert int to uint because int contains both positive and negative integers whereas uint works for positive values only.
uint() to int()

The implicit conversion from uint() to int() can be done as follow:

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0; contract SolidityTest{function uint_To_int(uint mrx) public pure returns(int){int ample=int(mrx);return ample;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
string to bytes()

The string can be converted to bytes using implicit conversion as follow:

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0; contract SolidityTest{function string_to_byte(string memory mrx) public pure returns(bytes memory){bytes memory ample = bytes(mrx); return ample; }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Explicit Conversion

Conversions that are explicitly performed by the developer are known as explicit conversions.

In situations where you cannot rely on implicit conversions, these conversions are necessary.

It is also necessary to use explicit conversions when you are performing operations on variables that are distinct to each other.

The constructor syntax allows us to explicitly convert data types from one to another.

int8 to uint:

The explicit conversion from int8 to uint can be done as:

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0; contract SolidityTest{function int8_To_uint(int8 mrx) public pure returns(uint){uint ample=uint8(mrx);return ample;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
uint32 to uint16:

To convert uint32 to uint16, do consider the example below:

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0; contract SolidityTest{function uint32_To_uint16(uint32 mrx) public pure returns(uint16){uint16 ample=uint16(mrx);return ample;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
uint16 to uint32:

In order to convert uint16 to uint32,refer to the example below:

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0; contract SolidityTest{function uint16_To_uint32(uint16 mrx) public pure returns(uint32){uint32 ample=uint32(mrx);return ample;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
bytes2 to bytes1:

The following example shows the conversion of bytes2 to byte1:

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0; contract SolidityTest{function bytes2_To_bytes1(bytes2 mrx) public pure returns(bytes1){bytes1 ample=bytes1(mrx);return ample;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
bytes1 to bytes2:

If you want to convert bytes to bytes2, you can follow the given example:

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0; contract SolidityTest{function bytes1_To_bytes2(bytes1 mrx) public pure returns(bytes2){bytes2 ample=bytes2(mrx);return ample;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

 

bytes2 to bytes4:

The given example converts bytes2 to bytes4:

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0; contract SolidityTest{function bytes2_To_bytes4(bytes2 mrx) public pure returns(bytes4){bytes4 ample=bytes4(mrx);return ample;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Conclusion

Solidity mappings offer a powerful way to store and retrieve key-value pairs efficiently.

Smart contracts use mappings in a variety of ways, such as tracking balances, tracking ownership, and storing data in a decentralized way.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *