Skip to main content
1 of 2

Encode strings into bytes and then decode back to strings

My goal is to encode two strings into bytes with this function:

function encode(string memory _address1, string memory _address2) public  returns (bytes memory) {
        encoded = abi.encodePacked(_address1, _address2);
        return encoded;

    }

And then I want to decode the bytes back into individual strings:

function decode(bytes memory _encoded) public pure returns (string memory x, string memory y) {
       return abi.decode(_encoded, (string, string));
    }  

However, when I feed the bytes from the encode function, I get the following error:

call to ERC721ReceiverMock.decode errored: VM error: revert.