0 votes
1 view
in Blockchain by (12.4k points)

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!

1 Answer

0 votes
by (29k points)

Hi, you can use the following:

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);
    }

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...