My solidity function is returning a bytes32[] which I am looping in Javascript and converting to string using web3.toAscii(). Printing this in console shows extra characters at the end as shown below

enter image description here

The function returning bytes32

function myBets() public view returns (uint[] memory, bytes32[] memory, bytes32[] memory, int[] memory){
    uint n = betterCacheData[msg.sender].length;

    uint[] memory betValues = new uint[](n);
    bytes32[] memory options = new bytes32[](n);
    bytes32[] memory winStatus = new bytes32[](n);
    int[] memory moneyLineValues = new int[](n);

    for(uint i=0; i<n; i++){
        uint betId = betterCacheData[msg.sender][i].betId;
        uint optionId = betterCacheData[msg.sender][i].optionId;

        betValues[i] = betterCacheData[msg.sender][i].betValue;
        options[i] = bets[betId].moneyLineData[optionId].option;

        if(bets[betId].betStatus == BetStatus.Open || bets[betId].betStatus == BetStatus.Closed){
            winStatus[i] = "pending";
        } else{
            if(bets[betId].correctChoice == optionId){
                winStatus[i] = "won";
                moneyLineValues[i] = bets[betId].moneyLineData[optionId].value;
            } else{
                winStatus[i] = "lost";
    return (betValues, options, winStatus, moneyLineValues);

Any help would be greatly appreciated! Thank you!

  • Show your full code please (the part relevant to this question). Nov 8, 2019 at 6:39
  • @goodvibration I have added the solidity function.
    – Stuxen
    Nov 8, 2019 at 6:59
  • Good, now can you please print those bytes32 values without web3.toAscii, and then post them here? Nov 8, 2019 at 7:23
  • @goodvibration 0x776f6e0000000000000000000000000000000000000000000000000000000000 and 0x6c6f737400000000000000000000000000000000000000000000000000000000 I just realized that all the byte32 values returned by the function have these weird characters. But these characters don't show up when I use these variables inside html page but only in the console.
    – Stuxen
    Nov 8, 2019 at 19:57

1 Answer 1


I used the byte32 values to convert to string using a hex to string converter website. The website also displays these weird characters.

original byte32 value

Then I tried stripping all characters before and including x, and also all the 0's and now I got the desired output

modified byte32 value

Then I tried passing 776fe to web3.toAscii() and got the desired output.

TLDR Use web3.toUtf8() instead

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.