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)

  • 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 '18 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. – Likhith Kumar Mar 21 '18 at 16:14
  • What is the error reported? How do you call the check function?. – Ismael Mar 21 '18 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) – Likhith Kumar Mar 21 '18 at 16:58
  • You can edit your own question and add the example there. – Ismael Mar 21 '18 at 17:43
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) {
}); 
  • what is the difference between return meta.check(acc, name, password) and return meta.check(acc, name, password,{from:account,gas:300000}); – Likhith Kumar Mar 22 '18 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 '18 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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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