Solidity Tutorial : all about Bytes

Jean Cvllr
11 min readAug 1, 2019

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 :

  1. Endianness & bytes data layout in Solidity
  2. Fixed-size byte arrays
  3. Dynamically-size byte arrays
  4. Bitwise operations in Solidity
  5. An array of bytes = a little difference
  6. Bytes as function arguments
  7. Conversion between addresses and bytes20
  8. Advanced operations with Bytes
  9. 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)
This is how a variable y with a value of 0x01234567 (hexa-decimal representation) would be stored in both formats

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 ( bytesN , uintN , address , etc…), the data is laid out differently. The…

--

--