Im calling a smart contract function that requires addresses, I only have these addresses as strings. How do I convert string to address in solidity?

3 Answers 3


In Solidity, an address is represented by a 20-byte value. You can convert a string that represents an Ethereum address to an address type using the address function. Here's an example:

function myFunction(string memory addressString) public {
  address myAddress = address(bytes20(bytes(addressString)));
  // Do something with myAddress

In this example, addressString is the string that represents an Ethereum address. The bytes(addressString) converts the string to a byte array, and bytes20 takes the first 20 bytes of that array. Finally, address casts the resulting 20-byte value as an Ethereum address.

Note that the input string must be a valid Ethereum address in order for this conversion to work properly. If the input string is not a valid Ethereum address, this code may result in an error.

Also, be aware that you may need to handle the possibility of invalid input strings in your contract code, either by validating the input before passing it to address, or by adding appropriate error handling code.


Answer by PSS does not seem to work for me. I am not getting the correct address back. But I am able to get the address back correctly using the following syntax address(0x33253420549e8E4a2Ea4061505BCFDBd288BA2f7) without any quotes


this is working perfectly for me. How to convert string into address.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract Check {
    address public pick;
     function toAddress(string calldata s) public  returns (address) {
        bytes memory _bytes = hexStringToAddress(s);
        require(_bytes.length >= 1 + 20, "toAddress_outOfBounds");
        address tempAddress;

        assembly {
            tempAddress := div(mload(add(add(_bytes, 0x20), 1)), 0x1000000000000000000000000)

        pick = tempAddress;
     function hexStringToAddress(string calldata s) public  returns (bytes memory) {
        bytes memory ss = bytes(s);
        require(ss.length%2 == 0); // length must be even
        bytes memory r = new bytes(ss.length/2);
        for (uint i=0; i<ss.length/2; ++i) {
            r[i] = bytes1(fromHexChar(uint8(ss[2*i])) * 16 +

        return r;


     function fromHexChar(uint8 c) public returns (uint8) {
        if (bytes1(c) >= bytes1('0') && bytes1(c) <= bytes1('9')) {
            return c - uint8(bytes1('0'));
        if (bytes1(c) >= bytes1('a') && bytes1(c) <= bytes1('f')) {
            return 10 + c - uint8(bytes1('a'));
        if (bytes1(c) >= bytes1('A') && bytes1(c) <= bytes1('F')) {
            return 10 + c - uint8(bytes1('A'));
        return 0;

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.