Use abi.encodePacked(x)
where x is the address. (Thanks @k06a)
Use abi.encodePacked(x)
where x is the address. (Thanks @k06a)
I was not able to read the ABI-encoded string with web3.js. Therefore, I added some conversion to the ASCII characters:
function toAsciiString(address x) internal pure returns (string memory) {
bytes memory s = new bytes(40);
for (uint i = 0; i < 20; i++) {
bytes1 b = bytes1(uint8(uint(uint160(x)) / (2**(8*(19 - i)))));
bytes1 hi = bytes1(uint8(b) / 16);
bytes1 lo = bytes1(uint8(b) - 16 * uint8(hi));
s[2*i] = char(hi);
s[2*i+1] = char(lo);
}
return string(s);
}
function char(bytes1 b) internal pure returns (bytes1 c) {
if (uint8(b) < 10) return bytes1(uint8(b) + 0x30);
else return bytes1(uint8(b) + 0x57);
}
Casting an address to string Solidity 8.0
solidity - how to convert string to Address? - Ethereum Stack Exchange
ethereum - Solidity: How to type cast string memory to address and uint type? - Stack Overflow
blockchain - How can we convert an Ethereum address (string) to Solidity's address type in Javascript in order to pass it to a contract function as an argument? - Stack Overflow
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
One issue with your code is that you are making a call, but your function is not constant and modifies the storage, you need to issue a standard transaction for that reason
CoinFlipper.deployed().then(function(instance) {
meta = instance;
return meta.check(acc, name, password); /// <--- without `call`
}).then(function (value) {
});
As already noted, that's not really recommended.
But if you reallyreally want to do it, have a look at Converting oraclize result from string to address (especially the answers which are not abour Oraclize).