When trying to compare a string from storage to a string being passed into a function (memory), I am getting the following error when attempting to convert the memory string using keccak256(). What am I supposed to be doing differently?

TypeError: Invalid type for argument in function call. Invalid implicit conversion from string memory to bytes memory requested. This function requires a single bytes argument. Use abi.encodePacked(...) to obtain the pre-0.5.0 behaviour or abi.encode(...) to use ABI encoding.

1 Answer 1


You should first convert the strings to bytes using abi.encode():

pragma solidity 0.5.4;

contract Test {
    string public constant text = "Hello world!";

    function isEqualTo(string memory myString) public pure returns(bool) {
        return (keccak256(abi.encode(text)) == keccak256(abi.encode(myString)));

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.