Conversion between addresses and bytes20
An Ethereum address is 20 bytes long, and you can convert it to a bytes20 type or vice versa. To convert an address to bytes20, you can use an explicit typecast.
Example:
Solidity
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract AddressBytes20Conversion { // Converts an Ethereum address to bytes20 function addressToBytes20(address addr) public pure returns (bytes20) { return bytes20(addr); } // Converts a bytes20 value to an Ethereum address function bytes20ToAddress(bytes20 bytes20Addr) public pure returns (address) { return address(bytes20Addr); } } |
Output:
Bytes in Solidity
In Solidity, the term “bytes” refers to a dynamically-sized byte array. Solidity provides two types of byte arrays: fixed-size arrays (called “bytesN”, where N is a number between 1 and 32) and dynamic arrays (simply called “bytes”).