8

Is there a simple way to convert a bytes32, e.g. 0xca35b7d915458ef540ade6068dfe2f44e8fa733c, to an address?

bytes32 data = "0xca35b7d915458ef540ade6068dfe2f44e8fa733c";

I tried casting it with address(data) but that didn't work. I've also seen this solution here but that only works with bytes and not bytes32.

Here's the function I need in solidity:

function bytes32ToAddress(bytes32 input) public pure returns(address){
   // convert input to address
   return ...;
}
Chris
  • 1,302
  • 12
  • 23
  • at the end of 2022 implicit conversion is not allowed anymore, but explicit conversion should be possible https://ethereum.stackexchange.com/a/126050/23579 – Gleichmut Dec 13 '22 at 13:51

4 Answers4

10

For solidity 0.5.x you can use

pragma solidity ^0.5.0;

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

First convert the bytes32 to a uint256, later to uint160(20 bytes) and finaly to addres, this use big endian.

If you want use little endian you should use address(uint160(bytes20(b)))

For more information: solidity doc

Alfredo Egaf
  • 321
  • 3
  • 9
2

This works fine. Perhaps you were actually including single quotes in your code? If so, remove them.

pragma solidity ^0.4.24;

contract Test {
    function test(bytes32 data) external pure returns (address) {
        return address(data);
    }
}
user19510
  • 27,999
  • 2
  • 30
  • 48
  • That's not what I am looking for. I edited the question. I want to convert a bytes32 element to an address. – Chris Jun 10 '18 at 20:06
  • So you have a string with a hexadecimal representation of an address in it? Why? – user19510 Jun 10 '18 at 20:06
  • Or if the double quotes are again a mistake, see my edited code. Converting from bytes32 to address is straightforward, but if you actually have a string, that's another matter. – user19510 Jun 10 '18 at 20:08
  • I have a function that accepts a bytes32 element. I don't know how to represent a bytes32 element inline. I just put it inside the remix-function in quotes. To be honest I don't even know what bytes32 is. – Chris Jun 10 '18 at 20:22
  • I made another edit to accept the bytes32 as a parameter. In any case, it all works the same. If you have a bytes32, you can just cast it to address. – user19510 Jun 10 '18 at 20:23
  • To pass a bytes32 in Remix, make sure to pad it appropriately. E.g. type 0x000000000000000000000000ca35b7d915458ef540ade6068dfe2f44e8fa733c. – user19510 Jun 10 '18 at 20:25
  • Ah I see! I missed the padding part. Now it works. Thank you! Do you have ressources to learn about bytes32? The documentation doesn't give much information. – Chris Jun 10 '18 at 20:28
  • bytes32 is just 32 bytes. – user19510 Jun 10 '18 at 20:37
  • I see. Sorry for beeing a noob. – Chris Jun 10 '18 at 20:40
  • 3
    Hey everyone, reopening the issue almost a year later, but when trying your code @smarx, I got this error: TypeError: Explicit type conversion not allowed from "bytes32" to "address".. Any idea how to solve this? – Thanh-Quy Nguyen Apr 26 '19 at 13:13
  • I assume you're using Solidity 0.5.x. There's another answer to this question that gives code for 0.5.x. – user19510 Apr 26 '19 at 13:39
2
assembly {
    mstore(0, hash)
    addr := mload(0)
}

Here is an example how to convert public key into address this way: Get address from public key in Solidity

Denis Glotov
  • 131
  • 3
1

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.

https://docs.soliditylang.org/en/v0.5.9/types.html#address

anonymous
  • 21
  • 5