2
function debug() returns (bytes) {
        bytes memory ret = new bytes(6);
        bytes memory word = new bytes(6);
        word = hex"61626f757400";
        assembly {
            mstore(add(ret, 32), word)
        }
        return ret;
}

returns 0x000000000000, desired outcome return 0x61626f757400

1

1 Answer 1

2

Notice how ret is the location in memory of the length of the bytes array ret. It's the same with word, so when you do mstore(add(ret, 32), word), you're actually storing the location in memory of the length of array word at the start of the data in the ret array. Here is the correct version of what you're trying to do:

function debug() view returns (bytes) {
    bytes memory ret = new bytes(6);
    bytes memory word = new bytes(6);
    word = hex"61626f757400";
    assembly {
        mstore(add(ret, 32), mload(add(word, 32)))
    }
    return ret;
}
1
  • 1
    of course, not sure why I missed that. Thanks, great answer.
    – lektion
    Commented Jul 21, 2018 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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