0

Is it possible to encode and decode two consequent bytes using abi.encode and abi.decode (as following)?

bytes memory data = abi.encode(data1, data2);
// later on
(bytes memory data1_recovered, bytes memory data2_recovered) = abi.decode(data, bytes, bytes);

1 Answer 1

1

It does work. The following example concatenates two strings.

function foo(string memory a, string memory b) public view returns (string memory) {
    bytes memory data1 = bytes(a);
    bytes memory data2 = bytes(b);
    bytes memory data = abi.encode(data1, data2);
    return bar(data);
}

function bar(bytes memory data) public view returns (string memory) {
    (bytes memory data1_recovered, bytes memory data2_recovered) = abi.decode(data, (bytes, bytes));
    return string(abi.encodePacked(data2_recovered, " ", data1_recovered));
}

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.