Created
February 24, 2022 14:55
-
-
Save jar-o/feb33bb6aa31eb21691026eea2fb514b to your computer and use it in GitHub Desktop.
Solidity - convert uint256 array to bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract Converter { | |
function u256a_tobytes(uint256[] memory arr) public pure returns (bytes memory) { | |
bytes memory b = new bytes(arr.length*32); | |
for (uint256 i = 0; i < arr.length; i++) { | |
uint256 x = arr[i]; | |
uint256 j = 32+(i*32); | |
assembly { | |
mstore(add(b, j), x) | |
} | |
} | |
return b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment