String Literals As with integer literals, their type can vary, but they are implicitly convertible to bytes if they fit, to bytes and to string.
How? How can I convert uint to bytes?
also is there any way to cast address-typed variable to bytes32?
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 communityString Literals As with integer literals, their type can vary, but they are implicitly convertible to bytes if they fit, to bytes and to string.
How? How can I convert uint to bytes?
also is there any way to cast address-typed variable to bytes32?
for conversion the simple bytes32("string");
would work!
The type conversions are mainly constrained by the internal representations. Fixed types convert to other fixed types and dynamic types (bytes, string) seem to convert. Internally, the dynamic types have a representation of length so conversion implies reorganization that we don't get by simple casting.
Fiddling:
pragma solidity 0.4.25;
contract Cast {
uint n;
string s;
address a;
function convert() public view {
bytes32 b1 = bytes32(n);
bytes32 b2 = bytes32(a);
bytes32 b4 = bytes32("string");
bytes memory b5 = bytes(s);
}
}
Hope it helps.