openlang

Solidity conversion failure from address into uint256

I made an array containing structs, then attempted to retrieve each account's values from that array. But passing an address variable holding msg.sender failed with that array because its type apparently cannot convert to uint256. What is the way to accomplish this?

The conversation continues 3

Follow the reasoning
  1. Permalink

    An explicit cast is possible:

    uint256 accountNumber = uint256(msg.sender);
    function encodeAccount(address account) constant returns (uint256) {
      return uint256(account);
    }
  2. Permalink

    Starting with Solidity v0.8, an explicit conversion directly from address into uint256 is no longer allowed.

    The replacement is:

    uint256 accountNumber = uint256(uint160(msg.sender));
    function encodeAccount(address account) internal pure returns (uint256) {
        return uint256(uint160(account));
    }
  3. Permalink

    Before Solidity v0.8.0 this form was allowed:

    pragma <0.8.0;
    
    return address(toUint(entry));

    From Solidity v0.8.0 onward use this instead:

    pragma ^0.8.0
    
    return address(uint160(toUint(entry)));
    • Reference material

    address(uint) and uint(address): these change width and type-category together. Use address(uint160(uint) and uint(uint160(address)) in their respective places instead.

    consult Solidity Documentation, v0.8.0 breaking changes

Other sides of the same question

  1. TypeScript Map initialization with string array entries

    I'm creating a Map whose entries each hold an array of strings. How can I declare its type and initialize it together in one…

    Replies 2Latest activity typescript
  2. Typescript Callable Contract

    Why does Typescript accept my function definition even though it differs from the interface signature, yet report a problem w…

    Replies 2Latest activity typescript
  3. Is a read-only List<Int> a snapshot?

    List<Int> is a read-only view, not a snapshot. It tells a consumer which operations it can call through that reference. It pr…

    Replies 4Latest activity kotlin