Since both types, uint256's and bytes32's space are 2^256. How to map an uint256 type integer into bytes32 type in Solidity ?
Asked
Active
Viewed 1.7k times
14
1 Answers
18
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);
}
eth
- 85,679
- 53
- 285
- 406
-
1
-
1@JanBeneš actually it still works in v0.8 - I used
v0.8.15+commit.e14f2714to compile this code and it worked. – Paul Razvan Berg Jun 28 '22 at 11:59
bytes32 banduint256 u, then we can dob = bytes32(u)andu = uint256(b). Sorry, can't check now. – Ismael Jun 24 '16 at 18:19