Arachnid / solidity-stringutils

Basic string utilities for Solidity
Apache License 2.0
1.12k stars 369 forks source link

Function to convert Integer to String #40

Open apoorvlathey opened 4 years ago

apoorvlathey commented 4 years ago

Something along the lines of:

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);
        for (uint j = 0; j < i; j++) {
            s[j] = reversed[i - 1 - j];
        }
        str = string(s);
    }

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