3

I know it has been asked a lot of times but the newest post about this I could find was about 1 year old. To me it seems quite possible that things have changed since then. So my question: Is there an easy way to convert bytes8/16/32 to a String value?

Here is the problem I face right now. Contract A takes one user-input per transaction as a bytes8/16/32 variable. The contract passes it to Contract B and stores it there. My getter function in Contract B returns this Bytes8/16/32 variable. So Contract A calls the getter-function of B if needed and successfully gets the bytes variable back. Now I simply want to turn it back to a readable string and put it on screen in a decoded way :) Somebody has an idea? Many thanks!

2

I'm assuming you're invoking your Ethereum contract from JavaScript. If that's the case, you probably need web3.toAscii()

var str = web3.toAscii("0x657468657265756d000000000000000000000000000000000000000000000000");
console.log(str); // "ethereum"
2

If your need for a readable string is just for the Web interface and you are using web3.js you can use :

var str = web3.toUtf8("0x657468657265756d000000000000000000000000000000000000000000000000");
console.log(str); // "ethereum"

web3.toAscii can still return some not desirable characters, and since UTF-8 is most used, i suggest the web3.toUtf8

An other way is to try and parse the bytes32 to a readable string inside your contract. to do so please refer to this question it has the best answer.

How to convert a bytes32 to string

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.