Tobytes funciona en solidez

function toBytes(uint256 x) private pure returns (bytes memory b) {
  b = new bytes(32);
  assembly { mstore(add(b, 32), x) }
}
akileus