5

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?

1
  • 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, 2016 at 7:43

2 Answers 2

7

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

1
  • If you have another question, please ask it by clicking the Ask Question button.
    – eth
    Jul 7, 2016 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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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