Solidity string type capacity size


foreword

Today, a friend asked what is the capacity of the solidity string type? He wants to pass a dynamic string value, the size of which is uncontrollable, and no information can be found on the Internet.


bytes and string data types

bytes: A byte array with a dynamic length, see Arrays. non-value type.

string: UTF-8 encoded character type with dynamic length, see Arrays. A non-value type [valueType].

Byte arrays (byte arrays)
Fixed-size byte arrays (Fixed-size byte arrays)
bytes1, ... , bytes32, the value is allowed to increase with a step size of 1. byte means byte1 by default.

Summarize

The principle of use is:

  • bytes is used to store byte data of arbitrary length, and can store small pictures, small audio files, etc. Large files are generally not directly stored on the chain, and only the hash code of large files is stored on the chain, and bytes are generally used to store characters String, because it saves space.
  • string is used to store UTF-8 encoded string data of any length. If the length can be determined, try to use a fixed length such as byte1 to byte32, because it saves space.
  • insert image description here

Guess you like

Origin blog.csdn.net/ws327443752/article/details/123702346