In Solidity, how can I convert the sender address to a string?
The comments in How to convert an address to bytes in Solidity? did not provide a working solution
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityIn Solidity, how can I convert the sender address to a string?
The comments in How to convert an address to bytes in Solidity? did not provide a working solution
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 view 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 view returns (bytes1 c) {
if (uint8(b) < 10) return bytes1(uint8(b) + 0x30);
else return bytes1(uint8(b) + 0x57);
}
if (b < 10)
has to be changed to if (uint8(b) < 10)
, then the Operator < not compatible with types bytes1 ...
error is fixed. I made an edit to the answer. Hope it gets approved.
– Jack O'neill
Sep 5 '19 at 13:35
This is method to convert address
to hex string
:
function toString(address account) public pure returns(string memory) {
return toString(abi.encodePacked(account));
}
function toString(uint256 value) public pure returns(string memory) {
return toString(abi.encodePacked(value));
}
function toString(bytes32 value) public pure returns(string memory) {
return toString(abi.encodePacked(value));
}
function toString(bytes memory data) public pure returns(string memory) {
bytes memory alphabet = "0123456789abcdef";
bytes memory str = new bytes(2 + data.length * 2);
str[0] = "0";
str[1] = "x";
for (uint i = 0; i < data.length; i++) {
str[2+i*2] = alphabet[uint(uint8(data[i] >> 4))];
str[3+i*2] = alphabet[uint(uint8(data[i] & 0x0f))];
}
return string(str);
}
This works with solidity 0.6.0
function addressToString(address _pool) public pure returns (string memory _uintAsString) {
uint _i = uint256(_pool);
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len - 1;
while (_i != 0) {
bstr[k--] = byte(uint8(48 + _i % 10));
_i /= 10;
}
return string(bstr);
}
returns (string memory _uintAsString)
!!! so it is a int, not the address string as you'd expect on first sight. This also means that if your address starts with one ore more zeros (0x0A1bC4...), then converting it to an int and then back to hex (address) again will result in this leading 0 missing. You'll need something like this: '0x' + BigInt(ethAddressAsInt).toString(16).padStart(40, '0')
(40 being the length of an ethereum address, excluding the "0x")
– Markus Kottländer
Dec 21 '20 at 2:39
internal pure
keyword, otherwise you will get a warning. For more information about pure function. – Kushan Gunasekera Jun 18 '18 at 11:12