1

I am new to ethereum. I am using truffle to develop, deploy and interact with my solidity contract.I have a function in my contract which is as follows:

  function getResult(bytes32 name) public view returns (bytes32, bytes32,uint) {
        return (name,nameArray[name].pName,pCount[name]);
    }

Using truffle console I accessed it as follows:

var result = poe.getResult('QWERTY');

**poe: is the contract instance

the output of the function is:

[ '0x5157455254590000000000000000000000000000000000000000000000000000',
  '0x456c656331000000000000000000000000000000000000000000000000000000',
  BigNumber { s: 1, e: 0, c: [ 1 ] } ]

Can anyone help me in converting the output to a readable format(hex to string and bignumber to integer)

Any leads would be appreciated. Thank you!!

2

the output is in array format. is there a way to extract the individual elements. I tried using toNumber and web3.toAscii() but doesn't seem to work

  • I tried using your method on similar function, but it returns undefined. var test = contractInstanceName.getList(). var convertedRes = web3.toAscii(test) :returns undefined – aib92 Sep 13 '18 at 19:38
2

You convert the BigNumber by just calling .toNumber() on it, and you can convert the hex string using web3's built-in web3.toAscii() function. Bonus points, web3's function automatically handles the leading 0x so you don't have to!

Edit: Note that this function returns three values, (name,nameArray[name].pName,pCount[name]), which is why you're getting an array. A full conversion would look like

convertedResult = [web3.toAscii(result[0]), web3.toAscii(result[1]), result[2].toNumber()]

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.