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"

0x4441490000000000000000000000000000000000000000000000000000000000

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 at 7:32
  • changing it to string works too :) – harambissimo Jul 10 at 16:40
up vote 0 down vote accepted

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

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.