blockchainsolidity

How to convert uint to string in solidity?


In Solidity, is there a way I can convert my int to string ?

Example:

pragma solidity ^0.4.4;

contract someContract {

    uint i;

    function test() pure returns (string) {

      return "Here and Now is Happiness!";

    }

    function love() pure returns(string) {

        i = i +1;

        return "I love " + functionname(i) + " persons" ;
    }



}

What is functionname?Thanks!


Solution

  • The two post here are giving the responses :

    https://ethereum.stackexchange.com/questions/10811/solidity-concatenate-uint-into-a-string

    https://ethereum.stackexchange.com/questions/10932/how-to-convert-string-to-int

    function uintToString(uint v) constant returns (string str) {
            uint maxlength = 100;
            bytes memory reversed = new bytes(maxlength);
            uint i = 0;
            while (v != 0) {
                uint remainder = v % 10;
                v = v / 10;
                reversed[i++] = byte(48 + remainder);
            }
            bytes memory s = new bytes(i + 1);
            for (uint j = 0; j <= i; j++) {
                s[j] = reversed[i - j];
            }
            str = string(s);
        }
    

    Regards