[Ethereum] bytes32 to uint conversion is incorrect

bytes32solidityweb3js

I instantiate the following contract with _number as 10. When I check the value of number, I get 72370055773322622139731865630429942408293740416025352524660990004945706024960. Is this a problem with my contract, or is this a web3 bug?

contract Thing{
  uint public number;
  function Thing(bytes32 _number){
    number = uint(_number);
  }
}

Best Answer

72370055773322622139731865630429942408293740416025352524660990004945706024960 is 0xa000000000000000000000000000000000000000000000000000000000000000L - or 10 in hex, left shifted. Without seeing your web3 code, it's difficult to say why it was parsed this way, but it seems likely web3 treated your input as a single byte byte string, and left-aligned it into the bytes32 argument.

Related Topic