1

I want to define a constant bytes32 like this:

bytes32 internal constant FOO = bytes32(0x01);

But I'm getting the following error when compiling the code above:

Explicit type conversion not allowed from "int_const 1" to "bytes32".

How can I do this in Solidity v0.8?

1 Answer 1

1

You just have to cast the constant values to uint256, like this:

bytes32 internal constant FOO = bytes32(uint256(0x01));

Note that you can also optionally ditch the hexadecimal prefix:

bytes32 internal constant FOO = bytes32(uint256(1));

Though you may want to keep it so that you remember that the value is a bytes32.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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