Solidity casting from "uint256" to "address"

Viewed 4056

I would like to know how I can convert a uint256 data type to address on Solidity latest versions.

Here's an example of the code that I'm developing.

function setDetails(string memory _name) public onlyAuthCaller returns(address){
        uint256 tmpData = uint256(keccak256(abi.encodePacked(msg.sender, block.timestamp)));
        address batchNo = address(tmpData);

        detailsData.name = _name;

        batchDetails[batchNo] = detailsData;

        nextAction[batchNo] = 'NEXT';

        return batchNo;
}

On Remix, I'm having a TypeError: Explicit type conversion not allowed from "uint256" to "address" on line: address batchNo = address(tmpData);

If someone could help me solve this error for casting these data types.

Regards

1 Answers

Might be because uint256 is too large, and needs to be truncated.

Taken from the docs: enter image description here

Related