0

I was using strings before I read that using bytes is gas cheaper. Now I changed my strings to bytes32 but I don't know how to compare them. I need to check if two bytes32 are equal in the require statement. How can I do it? Using normal == gives revert and surrouding it with keccak256 says it's invalid implicit conversion from bytes32 to bytes requested.

  • Could you post your code please? It seems to me that something else is wrong – Henk Feb 19 at 4:54
3

A comparison between two bytes32 IS possible in solidity. Consider this simple contract:

pragma solidity 0.5.4;

contract Test {
    bytes32 public constant bytes32_ = "Hello World!";
    bytes32 public constant anotherBytes32 = "Hello World!";

    function areTheyEqual() public pure returns(bool) {
        return (bytes32_ == anotherBytes32);
    }
}

This contract will compile and it will return the correct answer (true). Could you please post your code along with the question? The issue is somewhere else, not in the comparison itself.

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.