0

I have a function that is called with some data - "SOMETHING".

function checker(address _from, uint _value, bytes calldata _data) external{
    //HOW to check if _data contains "SOMETHING"?
}

I have already tried:

if(string(_data) == ("SOMETHING")){...};
if(string(_data) == ("0x534f4d455448494e470a")){...}; // SOMETHING in hex
if(_data == 0x534f4d455448494e470a){...}
if(_data == hex"SOMETHING"){...}
if(_data == hex"534f4d455448494e470a"){...}

but it always says

Operator == not compatible with types string calldata and literal_string 

in some form or the other...

How can I compare the calldata with a String value? How to convert calldata to String correctly? How to check if the calldata contains some String value?

0

You cannot compare bytes or string using == in Solidity. You can however compare bytes32, so a simple solution is to hash the bytes first using keccak256 and compare the result of that.

For example:

function checker(bytes calldata _data) external pure returns (bool) {
  return keccak256(_data) == keccak256(abi.encodePacked("SOMETHING"));
}

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.