0

According this documentation

types shorter than 32 bytes are neither zero padded nor sign extended and

When I use abi.encodePacked(address, uint, uint, address) I am getting a result with lot of zeros. For example:

address addr = "0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C";
uint x = 123;

abi.encodePacked(addr , x , x , addr)
0x14723a09acff6d2a60dcdf7aa4aff308fddc160c000000000000000000000000000000000000000000000000000000000000007b000000000000000000000000000000000000000000000000000000000000007b14723a09acff6d2a60dcdf7aa4aff308fddc160c

Shouldn't it be without zeros.

  • 1
    Change uint to uint8 and try it again – sea212 Sep 20 '18 at 14:02
2

uint is 256 bit, so everything is encoded correctly:

first parameter:  14723a09acff6d2a60dcdf7aa4aff308fddc160c
second parameter: 000000000000000000000000000000000000000000000000000000000000007b
third parameter:  000000000000000000000000000000000000000000000000000000000000007b
fourth parameter: 14723a09acff6d2a60dcdf7aa4aff308fddc160c

it should not be without zeros, it takes the whole 32 bytes.

0

I know your confusion, you might have some misunderstandings of the function "abi.encodePacked(...)".

As the document says, "types shorter than 32 bytes are not zero padded", but the type you use is uint, which is exactly a 32 bytes type.

Just try out the following code:

contract Test {

    function test() public pure returns (bytes memory, bytes memory) {
        int8 a = 1;
        uint16 b=2;
        return (abi.encode(a,b), abi.encodePacked(a,b));
    }
}

and the result will be:

0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002

and

0x010002

Note that I use int8 and uint16 in this contract, so the difference is that "abi.encode" will pad each parameter to 32 bytes, while "abi.encodePacked" will not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.