3

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.

1
  • Why do you need to convert a number to string? Commented Sep 15, 2019 at 4:34

2 Answers 2

3

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
1

You could also convert int_const to uint256 using OpenZeppelin's SafeCast library and then convert to bytes32

diff = bytes32(SafeCast.toUint256(11111));

https://docs.openzeppelin.com/contracts/3.x/api/utils#SafeCast

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.