A function recives bytes as input:

function foo (bytes data)

How do I parse that into individual byte32 chunks?

function foo (bytes data) returns (bytes32[])

My goal is to understand how to handle the Token function:

receiveApproval(address from, uint256 tokens, address token, bytes data)


You can loop over the bytes with an increment of 32:

function foo(bytes memory data) internal pure returns (bytes32[] memory output) {
    require(data.length % 32 == 0);
    output = new bytes32[](data.length / 32);
    for (uint256 i = 32; i <= output.length * 32; i += 32) {
        assembly {mstore(add(output, i), mload(add(data, i)))}
| improve this answer | |

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.