2

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?

1

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));
}
| improve this answer | |
  • 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.