23

I am trying to convert uint to string in solidity. Is it possible to convert? if so how can I convert it?

Convertion to base 10 is prefered, but in hexadecimal might also be helpful.

Ismael
  • 30,570
  • 21
  • 53
  • 96
madhan siva
  • 581
  • 1
  • 4
  • 8
  • 1
    You should specify what do you want as result, it is not unique. For instance: do you want an hex format output? Do you want a bin output? Do you want the base 10 number? Do you want an hex with uppercase for letter? And so on. – Rick Park Jan 30 '19 at 17:25

5 Answers5

17

Another option is to use a solution from Oraclize https://github.com/oraclize/ethereum-api/blob/master/oraclizeAPI_0.5.sol, it suits best for me:

0.5 Compiler Version:

    function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
    if (_i == 0) {
        return "0";
    }
    uint j = _i;
    uint len;
    while (j != 0) {
        len++;
        j /= 10;
    }
    bytes memory bstr = new bytes(len);
    uint k = len - 1;
    while (_i != 0) {
        bstr[k--] = byte(uint8(48 + _i % 10));
        _i /= 10;
    }
    return string(bstr);
}

Pre 0.5 Compiler Version:

function uint2str(uint i) internal pure returns (string){
    if (i == 0) return "0";
    uint j = i;
    uint length;
    while (j != 0){
        length++;
        j /= 10;
    }
    bytes memory bstr = new bytes(length);
    uint k = length - 1;
    while (i != 0){
        bstr[k--] = byte(48 + i % 10);
        i /= 10;
    }
    return string(bstr);
}
10

You can convert the uint to bytes32 by using bytes32 data = bytes32(u) (uint is same uint256 How to convert a uint256 type integer into a bytes32?)

Then use How to convert a bytes32 to string:

function bytes32ToString (bytes32 data) returns (string) {
    bytes memory bytesString = new bytes(32);
    for (uint j=0; j<32; j++) {
        byte char = byte(bytes32(uint(data) * 2 ** (8 * j)));
        if (char != 0) {
            bytesString[j] = char;
        }
    }
    return string(bytesString);
}
eth
  • 85,679
  • 53
  • 285
  • 406
  • I tried the above method, I am getting empty after conversion.
    uint256 test=1000;
    string val=bytes32ToString(bytes32(test));
    val i am getting as blank.
    – madhan siva Jun 28 '16 at 07:43
  • Please put your code on browser-solidity so people can check: http://ethereum.stackexchange.com/questions/2835/how-to-quickly-test-a-solidity-function – eth Jun 28 '16 at 07:48
  • consider below as my contract
    contract test {

    function testIntConversion() constant returns (string test) { test=bytes32ToString (bytes32(1234567)); } for this input I am getting output as string test: և. But what i wanted is functionality similar to var.toString() in java

    – madhan siva Jun 28 '16 at 08:01
  • what I needed is just typecasting from uint to string. – madhan siva Jun 28 '16 at 09:15
  • Solidity is character set agnostic concerning strings. I posted a separate answer that can help. – eth Jun 28 '16 at 22:23
8

Solidity is character set agnostic concerning strings. There is no built-in "toString" in Solidity. https://github.com/pipermerriam/ethereum-string-utils is a library that can help:

> stringUtils.uintToBytes(1234)
"1234"

The MIT licensed implementation is:

function uintToBytes(uint v) constant returns (bytes32 ret) {
    if (v == 0) {
        ret = '0';
    }
    else {
        while (v > 0) {
            ret = bytes32(uint(ret) / (2 ** 8));
            ret |= bytes32(((v % 10) + 48) * 2 ** (8 * 31));
            v /= 10;
        }
    }
    return ret;
}
eth
  • 85,679
  • 53
  • 285
  • 406
6

Dmitriy Vinokurov answer does not work in solidity 0.8.0. This is a revised version:

 function uint2str(
  uint256 _i
)
  internal
  pure
  returns (string memory str)
{
  if (_i == 0)
  {
    return "0";
  }
  uint256 j = _i;
  uint256 length;
  while (j != 0)
  {
    length++;
    j /= 10;
  }
  bytes memory bstr = new bytes(length);
  uint256 k = length;
  j = _i;
  while (j != 0)
  {
    bstr[--k] = bytes1(uint8(48 + j % 10));
    j /= 10;
  }
  str = string(bstr);
}
Tadej Vengust
  • 379
  • 4
  • 11
1

Based on the Oraclize code that Dmitry supplied I made a function to return a uint as a string in hex format:

function uint2hexstr(uint i) internal pure returns (string) {
    if (i == 0) return "0";
    uint j = i;
    uint length;
    while (j != 0) {
        length++;
        j = j >> 4;
    }
    uint mask = 15;
    bytes memory bstr = new bytes(length);
    uint k = length - 1;
    uint numStart = 48;
    uint letterStarn = 65;
    while (i != 0){
        uint curr = (i & mask);
        bstr[k--] = curr > 9 ? byte(55 + curr ) : byte(48 + curr); // 55 = 65 - 10
        i = i >> 4;
    }
    return string(bstr);
}

https://gist.github.com/okwme/f3a35193dc4eb9d1d0db65ccf3eb4034

okwme
  • 293
  • 2
  • 9