Clarify packed encoding.

This commit is contained in:
chriseth 2018-08-08 21:47:40 +02:00 committed by GitHub
parent d634d20b5b
commit f01689ac77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -565,14 +565,13 @@ Non-standard Packed Mode
Through ``abi.encodePacked()``, Solidity supports a non-standard packed mode where: Through ``abi.encodePacked()``, Solidity supports a non-standard packed mode where:
- no :ref:`function selector <abi_function_selector>` is encoded,
- types shorter than 32 bytes are neither zero padded nor sign extended and - types shorter than 32 bytes are neither zero padded nor sign extended and
- dynamic types are encoded in-place and without the length. - dynamic types are encoded in-place and without the length.
As an example encoding ``int1, bytes1, uint16, string`` with values ``-1, 0x42, 0x2424, "Hello, world!"`` results in :: As an example encoding ``int8, bytes1, uint16, string`` with values ``-1, 0x42, 0x2424, "Hello, world!"`` results in ::
0xff42242448656c6c6f2c20776f726c6421 0xff42242448656c6c6f2c20776f726c6421
^^ int1(-1) ^^ int8(-1)
^^ bytes1(0x42) ^^ bytes1(0x42)
^^^^ uint16(0x2424) ^^^^ uint16(0x2424)
^^^^^^^^^^^^^^^^^^^^^^^^^^ string("Hello, world!") without a length field ^^^^^^^^^^^^^^^^^^^^^^^^^^ string("Hello, world!") without a length field
@ -582,8 +581,7 @@ and dynamically-sized types like ``string``, ``bytes`` or ``uint[]`` are encoded
their length field. This means that the encoding is ambiguous as soon as there are two their length field. This means that the encoding is ambiguous as soon as there are two
dynamically-sized elements. dynamically-sized elements.
Note that constants will be packed using the minimum number of bytes required to store them.
This means that, for example, ``abi.encodePacked(0) == abi.encodePacked(uint8(0)) == hex"00"`` and
``abi.encodePacked(0x12345678) == abi.encodePacked(uint32(0x12345678)) == hex"12345678"``.
If padding is needed, explicit type conversions can be used: ``abi.encodePacked(uint16(0x12)) == hex"0012"``. If padding is needed, explicit type conversions can be used: ``abi.encodePacked(uint16(0x12)) == hex"0012"``.
Since packed encoding is not used when calling functions, there is no special support
for prepending a function selector.