javascript bytes32 to string

0 votes

currently i'm working on a ethereum dapp(voting), in my smart contract i have a function to fetch candidate list of type bytes32[] , on the java script side i'm not getting the values instead 0x only How to parse the value , below is the code

pragma solidity ^0.4.0;


contract Voting {


  mapping (bytes32 => uint8) public votesReceived;



  bytes32[] public candidateList;
string myString = "someString";



  function Voting(bytes32[] candidateNames) public {
    candidateList = candidateNames ;

  }


  function totalVotesFor(bytes32 candidate) view public returns (uint8) {
    return votesReceived[candidate];
  }

  function addCandidate(bytes32 candidate)  public returns (bool){
    require(isNewEntry(candidate));
    candidateList.push(candidate);
    return isNewEntry(candidate);
  }

  function voteForCandidate(bytes32 candidate) public {
    require(validCandidate(candidate));
    votesReceived[candidate] += 1;
  }

  function getCandidateList() view public returns (bytes32[]) {
return candidateList;
  }

  function isNewEntry(bytes32 candidate) view public returns (bool) {
    for(uint i = 0; i < candidateList.length; i++) {
        if (candidateList[i] == candidate) {
            return false;
        }
    }
    return true;
  }

  function validCandidate(bytes32 candidate) view public returns (bool) {
    for(uint i = 0; i < candidateList.length; i++) {
      if (candidateList[i] == candidate) {
        return true;
      }
    }
    return false;
  }
}

below is the code to access the contract function

Voting.deployed().then(function(contractInstance) {
    contractInstance.getCandidateList.call().then(function(v) {
    console.log(v)
    });
  })

someone please help me

Oct 8, 2018 in Blockchain by slayer
• 29,370 points

edited Oct 16, 2018 by Omkar 3,326 views

1 answer to this question.

0 votes

Assuming you're using web3 on the JS side, it's web3.toAscii.

Example from the docs:

var str = web3.toAscii("0x657468657265756d000000000000000000000000000000000000000000000000");
console.log(str); // "ethereum"
answered Oct 8, 2018 by Omkar
• 69,220 points

Related Questions In Blockchain

0 votes
1 answer

How to Check if string is in list in javascript?

At first, make sure that the string ...READ MORE

answered Sep 19, 2018 in Blockchain by slayer
• 29,370 points
3,142 views
+1 vote
2 answers

How to convert INT to STRING in Solidity?

Look at the following code : function uintToString(uint ...READ MORE

answered Jun 27, 2018 in Blockchain by Christine
• 15,790 points
12,489 views
0 votes
1 answer

how can i convert stub.GetTxTimestamp() to type string?

You can not directly convert the timestamp ...READ MORE

answered Jul 5, 2018 in Blockchain by digger
• 26,740 points
1,533 views
0 votes
1 answer
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,941 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,466 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,458 views
0 votes
1 answer

TypeError: argument must be a string node.js while encoding to hex string.

The error is in protobuf.Transaction.create headerSignature needs to be a string, ...READ MORE

answered Nov 6, 2018 in Blockchain by Omkar
• 69,220 points
3,326 views
0 votes
1 answer

Hyperledger Composer: How to query using string?

You can try this: query selectReading { ...READ MORE

answered Nov 9, 2018 in Blockchain by Omkar
• 69,220 points
589 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP