1

I have a function that takes bytes32 _name as a parameter and store it in a struct.

Let's say _name is either Chinese, Korean, or Japanese and from app.js I would convert the _name to hex to pass it to the function.

App.contracts.Test.deployed().then(function(instance) {
            return instance.storeName(web3.toHex(name), { from: account });

Now, when I retrieve the name by calling the function and convert it using web3.toAscii

App.contracts.Test.deployed().then(function(instance) {                 
        return instance.getStruct.call();
    }).then(function(result) {

var name = web3.toAscii(result[1]);

the value would look like ÁÁ8È. Is it something that web3.toAscii cannot handle other languages or should I just use string _name?

2

The red flag here is of course that you are using web3.toAscii to convert Chinese, Korean, etc. Ascii by default is restricted to the English language.

Solidity supports utf-8 and yes you can get and read names which are in utf-8 encoding (which allows non-English characters). The solution is to encode into utf-8 and then rest is all covered by web3.

var chinese_string = '你好'; //hello var chinese_string_utf_encoded = '\xE4\xBD\xA0\xE5\xA5\xBD'; //checkout utf-8js

web3.toHex(a_utf_encoded); //returns "0xe4bda0e5a5bd"

Now use web3.toUtf8 to decode it back to the original Chinese text.

web3.toUtf8("0xe4bda0e5a5bd"); //returns "你好"

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.