How do I convert an "int" type value (including negative integers) to a "string" type value in Solidity? I suppose I could convert it into a bytes32, and then convert it to a string, but I feel that it would cost a lot of gas. What is the least expensive way to do this?

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.


Hey there is no explicit int to string conversion in solidity. You could use a function like:

function toBytes(int x, int digits) returns (bytes b) {
    b = new bytes(digits);
    for (uint i = 0; i < digits; i++)
        b[i] = byte(uint8(uint(x));
  • Please focus on an answer for the actual question. Thanks :) – eth Jul 5 '16 at 17:22

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.