'Solidity-Type address not convertible to type uint256
I created an array of structures and then tried to get the values of each account of an array. But I failed with an array while passing the address variable which contains msg.sender
and the type is not visibly convertible to uint256
. How can I do it?
Solution 1:[1]
As of Solidity v0.8, you can no longer cast explicitly from address
to uint256
.
You can now use:
uint256 i = uint256(uint160(address(msg.sender)));
function f(address a) internal pure returns (uint256) {
return uint256(uint160(a));
}
Solution 2:[2]
You can cast it explicitly:
uint256 i = uint256(msg.sender);
function f(address a) constant returns (uint256) {
return uint256(a);
}
Solution 3:[3]
Pre v0.8.0 of Solidity you could do:
pragma <0.8.0;
return address(toUint(item));
Post v0.8.0 of Solidity you must now do:
pragma ^0.8.0
return address(uint160(toUint(item)));
- References
address(uint)
and uint(address)
: converting both type-category and width. Replace this by address(uint160(uint)
and uint(uint160(address))
respectively.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | cfly24 |
Solution 2 | Zvezdomir Zlatinov |
Solution 3 | Sam Bacha |