Ethereum Stack Exchange is a question and answer site for users of Ethereum, the crypto value and blockchain-based consensus network. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In many Solidity examples I read that use strings for parameters or return values, I see they are typed as bytes32 although there's a string type. What is the real reason for that? Thanks.

share|improve this question

2 main reasons:

  1. Contracts currently cannot read a string that's returned by another contract.
  2. The EVM has a word-size of 32 bytes, so it is "optimized" for dealing with data in chunks of 32 bytes. (Compilers, such as Solidity, have to do more work and generate more bytecode when data isn't in chunks of 32 bytes, which effectively leads to higher gas cost.)
share|improve this answer
    
Would this mean that if using string instead of byte32 this would make the contract split the string in 32 bytes chunk and then have an unpredictable number of chunks and thus have an unpredictable gas usage on this part of the code? – NMassart yesterday
1  
Sounds correct to me, and the unpredictable gas usage is another good consideration. – eth yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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