bytes4
is a commonly needed data type in Solidity, due to its use in msg.sig
and, more broadly speaking, in function selectors (e.g. Contract.func.selector
).
How can one convert bytes4
to bytes memory
?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityabi.encodeWithSelector
can be used for this:
bytes memory a = abi.encodeWithSelector(msg.sig);
bytes memory b = abi.encodeWithSelector(Contract.func.selector);
You can do it directly using abi.encode()
or abi.encodePacked()
.
function get(bytes4 _val) public pure returns(bytes memory) {
return abi.encode(_val);
}
Example:
Input: 0x60c06040
Output: 0x60c0604000000000000000000000000000000000000000000000000000000000
bytes memory convertedData = new bytes256(msg.sig)
?bytes256
doesn't exist as a type. But, assuming you meantbytes32
, no, you can't do that.