I'm working with an ERC20 token and when I instantiate a new instance of it and try to receive its symbol in my tests, I receive a hex representation of the string.

const contract = await ERC20.new("DAI")
const contractSymbol = await contract.symbol.call()

The relevant code for the ERC20 token is the following

bytes32 public symbol;

constructor(bytes32 symbol_) public {
    symbol = symbol_;

When I log out contractSymbol I get the following instead of "DAI"


Any explanations and suggestions are appreciated!

  • Someone correct me if I'm wrong (this is just an observation), the reason you are seeing it like that is because the EVM is big-endian (I think that's the proper way to say that). And that's why you're seeing the most significant bytes from left to right. As for why you are receiving a hex representation, what happens when you change bytes32 public symbol; to string public symbol; I'm assuming this has something to do with how web3 spits back the data to you. Because it is the correct value, nonetheless. – John DeBord Jul 10 '18 at 7:32
  • changing it to string works too :) – harambissimo Jul 10 '18 at 16:40

Your variable is a bytes32. You need to convert it in a string.

Use web3.utils.toAscii(val) to do the convertion. You can also use an online converter like this one (but remove the 0x prefix).

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.