In solc 0.4.28 I was able to do:
uint foo = 42;
bytes28 bar = bytes28(foo);
but in 0.5.9 I'm getting the error:
TypeError: Explicit type conversion not allowed from "uint256" to "bytes28".
bytes28 bar = bytes28(foo);
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityIn solc 0.4.28 I was able to do:
uint foo = 42;
bytes28 bar = bytes28(foo);
but in 0.5.9 I'm getting the error:
TypeError: Explicit type conversion not allowed from "uint256" to "bytes28".
bytes28 bar = bytes28(foo);
I figured it out... You have to cast the uint to the same length so in this case it would be:
bytes28 bar = bytes28(uint224(foo));
If it were bytes16
it would be:
bytes16 bar = bytes28(uint128(foo));
bytes vs bits:
28 / 32 == 224 / 256
16 / 32 == 128 / 256