Since both types, uint256's and bytes32's space are 2^256. How to map an uint256 type integer into bytes32 type in Solidity ?
1 Answer
Just use bytes32(u)
. It's as easy as that because they are both 2^256 (unlike bytes
).
No need for a separate function, but to clarify:
function convert(uint256 n) returns (bytes32) {
return bytes32(n);
}
-
1
-
1@JanBeneš actually it still works in v0.8 - I used
v0.8.15+commit.e14f2714
to compile this code and it worked. Commented Jun 28, 2022 at 11:59
bytes32 b
anduint256 u
, then we can dob = bytes32(u)
andu = uint256(b)
. Sorry, can't check now.