0

I want to convert string data to address type.I know that we can directly use address type in the parameters, but for some reason I want to convert it from string to address.

sample code

function check(string _address,string _name,string _password) public returns (string)
{

    address add=address(_address);
   if(!checkUser(add))
    {    
        Useraccnts.push(add)-1;
          Users  storage myuser=users[add];
          myuser.name=_name;
          myuser.password=_password;

        return "true";
    }
    else
    {
        return "false";
    }         

}

I got JSON-RPC: internal error while executing this function. so I want to check whether it would be resolved if I change the address to a string. If you know how to resolve the JSON-RPC error please help me out.

registerUser: function() { 
    var self = this; 
    var acc=account; 
    var name=document.getElementById("name").value; 
    var password=document.getElementById("password").value; 
    var meta; 
    CoinFlipper.deployed().then(function(instance) { 
        meta = instance; 
        return meta.check.call(acc,name,password).then(function (value) { }, 

I'm using the above function in app.js.

Error: Invalid JSON RPC response: {"id":6,"jsonrpc":"2.0","error":{"code":-32603}} at Object.InvalidResponse (app.js:10374)

5
  • 2
    I'd recommend to pass _address as address instead of string: function check(address _address,string _name,string _password) so the parsing is done in the client side.
    – Ismael
    Mar 21, 2018 at 15:47
  • Thank you @Ismael! actually, I got JSON-RPC: internal error while executing this function. so I want to check whether it would be resolved if I change the address to a string. If you know how to resolve the JSON-RPC error please help me out. Mar 21, 2018 at 16:14
  • What is the error reported? How do you call the check function?.
    – Ismael
    Mar 21, 2018 at 16:32
  • registerUser: function() { var self = this; var acc=account; var name=document.getElementById("name").value; var password=document.getElementById("password").value; var meta; CoinFlipper.deployed().then(function(instance) { meta = instance; return meta.check.call(acc,name,password).then(function (value) { }, I'm using the above function in app.js.Error: Invalid JSON RPC response: {"id":6,"jsonrpc":"2.0","error":{"code":-32603}} at Object.InvalidResponse (app.js:10374) Mar 21, 2018 at 16:58
  • You can edit your own question and add the example there.
    – Ismael
    Mar 21, 2018 at 17:43

3 Answers 3

1

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) {
}); 
2
  • what is the difference between return meta.check(acc, name, password) and return meta.check(acc, name, password,{from:account,gas:300000}); Mar 22, 2018 at 21:40
  • If you have more doubts it is better to create a new question, so there's more chances it being answered quickly. The difference is the second one you can specify max gas used and the sender account.
    – Ismael
    Mar 23, 2018 at 3:25
1

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).

0
pragma solidity ^0.8.0;

contract StringToAddress {

function stringToAddress(string memory _address) public pure returns (address) {
    string memory cleanAddress = remove0xPrefix(_address);
    bytes20 _addressBytes = parseHexStringToBytes20(cleanAddress);
    return address(_addressBytes);
}

function remove0xPrefix(string memory _hexString) internal pure returns (string memory) {
    if (bytes(_hexString).length >= 2 && bytes(_hexString)[0] == '0' && (bytes(_hexString)[1] == 'x' || bytes(_hexString)[1] == 'X')) {
        return substring(_hexString, 2, bytes(_hexString).length);
    }
    return _hexString;
}

function substring(string memory _str, uint256 _start, uint256 _end) internal pure returns (string memory) {
    bytes memory _strBytes = bytes(_str);
    bytes memory _result = new bytes(_end - _start);
    for (uint256 i = _start; i < _end; i++) {
        _result[i - _start] = _strBytes[i];
    }
    return string(_result);
}

function parseHexStringToBytes20(string memory _hexString) internal pure returns (bytes20) {
    bytes memory _bytesString = bytes(_hexString);
    uint160 _parsedBytes = 0;
    for (uint256 i = 0; i < _bytesString.length; i += 2) {
        _parsedBytes *= 256;
        uint8 _byteValue = parseByteToUint8(_bytesString[i]);
        _byteValue *= 16;
        _byteValue += parseByteToUint8(_bytesString[i + 1]);
        _parsedBytes += _byteValue;
    }
    return bytes20(_parsedBytes);
}

function parseByteToUint8(bytes1 _byte) internal pure returns (uint8) {
    if (uint8(_byte) >= 48 && uint8(_byte) <= 57) {
        return uint8(_byte) - 48;
    } else if (uint8(_byte) >= 65 && uint8(_byte) <= 70) {
        return uint8(_byte) - 55;
    } else if (uint8(_byte) >= 97 && uint8(_byte) <= 102) {
        return uint8(_byte) - 87;
    } else {
        revert(string(abi.encodePacked("Invalid byte value: ", _byte)));
    }
}
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.