0

Hi all I need tho sha256 two strings. I am new to solidity. This is what I've got:

function addressToString(address _addr) public pure returns(string memory) 
{
    bytes32 value = bytes32(uint256(_addr));
    bytes memory alphabet = "0123456789abcdef";

    bytes memory str = new bytes(51);
    str[0] = '0';
    str[1] = 'x';
    for (uint256 i = 0; i < 20; i++) {
        str[2+i*2] = alphabet[uint8(value[i + 12] >> 4)];
        str[3+i*2] = alphabet[uint8(value[i + 12] & 0x0f)];
    }
    return string(str);
}

function addExam(string memory examHash) public onlyOwner returns (string memory examProfessorHash) {
    // Hash the examHash and the professorsAddress together
    examsFinalHash = keccak256(abi.encodePacked(examHash, addressToString(msg.sender)));  // TYPE ERROR

    professorsExam[msg.sender] = examsFinalHash;

    return examsFinalHash;
}

I got the addressToString function from this SO answer: Convert address to string . But I still get this error:

Type bytes32 is not implicitly convertible to expected type string storage ref. examsFinalHash = keccak256(abi.encodePacked(examHash, addressToString(msg.sender))); // TYPE ERROR

1

The problem is that you try to assign the result of the keccak256 operation, which is of type 'bytes32', to the variable 'examsFinalHash', which is of type 'string storage ref'. Wherever your declare 'examsFinalHash', it must be of type bytes32 to store the result. bytes32 examsFinalHash;

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.