Solidity Errors – How to Solve Explicit Type Conversion Not Allowed from int_const 11111 to bytes32

solidity

I have the following code snippet:

pragma solidity ^0.5.1;
  contract Puzzle {
     address payable public owner ;
     bool public locked ;
     uint public reward ;
     bytes32 public diff ;
     bytes public solution ;


    constructor () public {
       owner = msg.sender ;
       reward = msg.value ;
       locked = false ;
       diff = bytes32 (11111); // pre - defined difficulty
    }

I am getting the following error:

solc Puzzle_sha256.sol Puzzle_sha256.sol:14:12: Error: Explicit type conversion not allowed from "int_const 11111" to "bytes32".
diff = bytes32 (11111); // pre – defined difficulty
^————-^

Some body please guide me.

Zulfi.

Best Answer

First, convert int_const to uint256 then convert it to bytes32.

diff = bytes32 (uint256(11111));

and there is another problem with your contract.

You are using msg.sender and msg.value in your constructor. For that you need to mark your constructor as payable.

constructor() payable public