Bytes are a large aspect of Solidity, whether when using fixed size
bytesN or dynamic size
bytes array. We will see in this article their difference, bitwise operations related to fixed-sized array
bytesN and we can use them to perform some generic methods like concatenating.
Table of Content :
- Endianness & bytes data layout in Solidity
- Fixed-size byte arrays
- Dynamically-size byte arrays
- Bitwise operations in Solidity
- An array of bytes = a little difference
- Bytes as function arguments
- Conversion between addresses and bytes20
- Advanced operations with Bytes
- A word of warning with Solidity bytes
1. Endianness & bytes data layout in Solidity
Introduction to Endianness
In computing, the term endianness corresponds to how the low level bytes of any data are stored and ordered. Therefore, it defines the internal ordering of the computer or machine memory.
We refer to multi-byte data types data that its t(uint, float, string, etc…). There are two ways for ordering multi-byte data types in computer: in little-endian or big-endian format (where format = order).
- With Big-Endian, the first byte of binary representation of the multibyte data-type is stored first.
- With Little-Endian, the last byte of binary representation of the multibyte data-type is stored first. (Intel x86 machines)
Bytes data layout in Solidity
Ethereum and the EVM is a Virtual Machine that uses the Big Endian format. In the EVM, all data (regardless of its Solidity type) is stored big-endian at the low level inside the virtual machine. Most importantly, the EVM uses 32 bytes words to manipulate data.
However, depending on its type (
address , etc…), the data is laid out differently. The…