0

I am unable to solve the error mentioned below.
Error: Data location must be "memory" for return parameter in function, but none was given. function fromHex(string memory s) public pure returns(bytes)

Please help to solve the same. Here is the code:

pragma solidity ^0.5.0;
contract SampleContract
{
    function fromHexChar(uint c) public pure returns(uint)
    {
        if(byte(c) >= byte('0') && byte(c) <= byte('9')) 
        {
            return c - uint(byte('0'));
        }
        if(byte(c) >= byte('a') && byte(c) <= byte('f'))
        {
            return 10 + c - uint(byte('a'));
        }
        if(byte(c) >= byte('A') && byte(c) <= byte('F'))
        {
            return 10 + c - uint(byte('A'));
        }
    }

    function fromHex(string memory s) public pure returns(bytes) 
    {
        bytes memory ss = bytes(s);
        require(ss.length%2 == 0);

        bytes memory r = new bytes(ss.length/2);
        for(uint i=0; i<ss.length/2; ++i)
        {
            r[i] = byte(fromHexChar(uint(ss[2*i]))*16 +         
            fromHexChar(uint(ss[2*i+1])));
        }
        return r;
    }
}

1 Answer 1

1
function fromHex(string memory s) public pure returns(bytes memory)
2
  • Change is causing few more errors of types conversion. like Error: Explicit type conversion not allowed from "uint256" to "bytes1". if(byte(c) >= byte('0') && byte(c) <= byte('9')) Error: Explicit type conversion not allowed from "bytes1" to "uint256". return c - uint(byte('0')); Commented May 23, 2019 at 16:47
  • These new errors are not caused by adding memory identifier, they only appear after resolving the first error. Are you sure about what these functions do? fromHexChar receives an uint and returns a uint, it's not like a correct type conversion. Commented Sep 8, 2019 at 19:14

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.