Skip to content

Instantly share code, notes, and snippets.

@jar-o
Created February 24, 2022 14:55
Show Gist options
  • Save jar-o/feb33bb6aa31eb21691026eea2fb514b to your computer and use it in GitHub Desktop.
Save jar-o/feb33bb6aa31eb21691026eea2fb514b to your computer and use it in GitHub Desktop.
Solidity - convert uint256 array to bytes
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