9

On a Smart Contract called "mem" there is a function called "get()" that returns a bytes32 that is supposed to be a string. How can I decode the string? The result is an hex value. I have tried toString() on geth console, but I am probably doing something wrong...

mem.get(); "0x4d61726b65745061792e696f206973206465706c6f79696e6720536d61727420" mem.get().toString(); "0x4d61726b65745061792e696f206973206465706c6f79696e6720536d61727420" mem.get().toString('hex'); "0x4d61726b65745061792e696f206973206465706c6f79696e6720536d61727420"

11

You can use web3.toAscii(hexString) as documented in Web3 JavaScript Ðapp API:

> web3.toAscii("0x4d61726b65745061792e696f206973206465706c6f79696e6720536d61727420")
"MarketPay.io is deploying Smart "

You can also use web3.toUtf8(hexString):

> web3.toUtf8("0x4d61726b65745061792e696f206973206465706c6f79696e6720536d61727420")
"MarketPay.io is deploying Smart "
| improve this answer | |
  • Thx. It works like a charm! – Juan Ignacio Pérez Sacristán Sep 27 '16 at 12:19
  • For version 1.x, it's under web3.utils. So you need to do web3.utils.toAscii(hexString) – MeetM May 10 at 16:03

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