0

I have bytes32 address as shown below: '0x000000000000000000000000697cb3a91d22f4cb39aeea7eb4a410fedd0bbe06'

How can I convert the above given bytes 32 in an address as shown below: 0x697cB3a91d22F4cB39AeEA7Eb4A410FeDD0bBE06

2
  • did you try to simply cast it : address(bytesString) Commented Jul 26, 2021 at 8:14
  • gives error TypeError: Explicit type conversion not allowed from "bytes32" to "address" Commented Jul 26, 2021 at 9:55

1 Answer 1

2

I guess this will work fine:

pragma solidity >=0.4.16 <0.9.0;

contract Convertion {
    function test(bytes32 _input) external pure returns (address) {
        return address(uint160(uint256(_input)));
    }
}

Refer Solidity doc:

If you convert a type that uses a larger byte size to an address, for example bytes32, then the address is truncated. To reduce conversion ambiguity version 0.4.24 and higher of the compiler force you make the truncation explicit in the conversion. Take for example the address 0x111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFFCCCC.

You can use address(uint160(bytes20(b))), which results in 0x111122223333444455556666777788889999aAaa, or you can use address(uint160(uint256(b))), which results in 0x777788889999AaAAbBbbCcccddDdeeeEfFFfCcCc.

1
  • 1
    any idea how we can do the same with web3.js? Commented Jul 26, 2021 at 9:56

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.