7

I have a contract that returns a bytes32 type variable and I'm currently testing it with truffle and javascript. The original data

['Roberto', 'Juan', 'Andrea']

This array is what i get as a response from my contract:

[ '0x526f626572746f00000000000000000000000000000000000000000000000000',
  '0x4a75616e00000000000000000000000000000000000000000000000000000000',
  '0x416e647265610000000000000000000000000000000000000000000000000000' ]

And this my code

let cands = [];
let length = Number(await voting.candidateListLength());
for (let i = 0; i < length; i++)
    cands.push(web3.toAscii(await voting.candidateList(i)));
console.log(cands);

Using web3.toAscii() method on each element to a obtain a human-friendly string produces the following:

[ 'Roberto\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000',
  'Juan\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000',
  'Andrea\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000' ]

Is there a way to remove the extra characters that come along with the original data?

2
  • Why does it return bytes32 and not string? (in which case, you could simply get rid of the web3.toAscii on the client side). May 8, 2018 at 8:04
  • because between contracts strings cannot be returned, and i'm trying to find a way to pass data that can be represented as strings for the end user of my Dapp
    – Roberto C.
    May 8, 2018 at 15:19

2 Answers 2

12

web3.toUtf8() will do the magic for you

2
  • is that used in which web3 version?
    – mirg
    May 9, 2018 at 2:25
  • I used this for web3 0.20.1, for web3 1.0.0-beta.26 using web3.utils.hexToUtf8 solved it
    – Roberto C.
    May 9, 2018 at 3:18
2

The web3.toAscii will not do that for you. This is the original code (ref):

var str = "";
var i = 0, l = hex.length;
if (hex.substring(0, 2) === '0x') {
  i = 2;
}
for (; i < l; i+=2) {
  var code = parseInt(hex.substr(i, 2), 16);
  str += String.fromCharCode(code);
}

but nobody stops you to create your own function to prevent this behavior. Example:

function toAscii(hex) {
  var str = "";
  var i = 0, l = hex.length;
  if (hex.substring(0, 2) === '0x') {
    i = 2;
  }
  for (; i < l; i+=2) {
    var code = parseInt(hex.substr(i, 2), 16);
    if(code != 0) {
      str += String.fromCharCode(code);
    }
  }

  return str;
}

web3 1.0 should fix this issue ref using web3.utils.hexToUtf8

2
  • So i would export this and then use within my other files, right?
    – Roberto C.
    May 8, 2018 at 15:42
  • or you use web3 1.0 that has this function web3.utils.hexToUtf8
    – mirg
    May 9, 2018 at 2:24

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.