2

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?

  • It is preferred if you can post separate questions instead of combining your questions into one. That way, it helps the people answering your question and also others hunting for at least one of your questions. Thanks! – eth Jul 7 '16 at 7:43
5

for conversion the simple bytes32("string"); would work!

  • If you have another question, please ask it by clicking the Ask Question button. – eth Jul 7 '16 at 7:44
1

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.

  • any idea how to do this in solc 0.5.x? – okwme Sep 22 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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