Since both types, uint256's and bytes32's space are 2^256. How to map an uint256 type integer into bytes32 type in Solidity ?

  • Possible duplicate of How to convert a uint to bytes in Solidity? Jun 24 '16 at 17:26
  • @RichardHorrocks It doesn't seem to be exactly the same question.
    – Ismael
    Jun 24 '16 at 18:18
  • 1
    I think that if we have bytes32 b and uint256 u, then we can do b = bytes32(u) and u = uint256(b). Sorry, can't check now.
    – Ismael
    Jun 24 '16 at 18:19
  • 1
    I'm being a bit trigger-happy with the dupes. Apologies - I'll reel it in. Credit to @Ismael. Jun 24 '16 at 22:07

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.