15

How would I concatenate an integer into a string using solidity?

For instance, say I have the following:

uint myInteger = 12

How can I create the following string "My integer is: 12" with 12 being the value of myInteger.

In other words have, "My integer is: " + myInteger

q9f
  • 32,913
  • 47
  • 156
  • 395
Pabi
  • 1,129
  • 4
  • 11
  • 18
  • 1
    Related, and possibly the solution: http://ethereum.stackexchange.com/q/729/87 – q9f Dec 22 '16 at 11:32

3 Answers3

9

This is my first answer here :)

  1. You can use openzeppelin's Strings.sol, and use toString(value) method to convert a number to a string.

toString(value): Converts a uint256 to its ASCII string decimal representation.

  1. Now to concat, we can use string.concat() [available after solidity version 0.8.12].

it'll look like this:

pragma solidity ^0.8.12;
import "@openzeppelin/contracts/utils/Strings.sol";

contract NumToStr {

function testConcat(uint256 num1) public pure returns (string memory) {
    return string.concat("The number is: ",Strings.toString(num1), ", Cool!!");
}

}

there are other methods in openzeppelin's Strings.sol like:

toHexString(value)

toHexString(value, length)

toHexString(addr)

  • You can read more about the Strings library in openzeppelin's docs here:

  • Instead of string.concat() we can also use abi.encodePacked(str1, str2), which returns bytes (see here). Then we can typecast the bytes to string with string(bytesVal):

function testConcat(uint256 num1) public pure returns (string memory) {
        return string(abi.encodePacked("The number is: ",Strings.toString(num1), ", Cool!!"));
    }

But, I know this approach is kinda hacky.

Saswata Dutta
  • 137
  • 1
  • 6
  • I have tried your approach (which I think is the simplest one) but for some reason is throwing me the following error:

    "TypeError: Type bytes memory is not implicitly convertible to expected type string memory."

    I'm using the Solidity compiler v8.9

    – mcarlomagno Dec 15 '22 at 16:20
7

Try the following:

pragma solidity ^0.4.4;

contract TestIntToString {

    string public result;

    function TestIntToString() {
        // result = uintToString(12345678901234567890);
        result = appendUintToString("My integer is: ", 1234567890);
    }

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

    function appendUintToString(string inStr, 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 inStrb = bytes(inStr);
        bytes memory s = new bytes(inStrb.length + i);
        uint j;
        for (j = 0; j < inStrb.length; j++) {
            s[j] = inStrb[j];
        }
        for (j = 0; j < i; j++) {
            s[j + inStrb.length] = reversed[i - 1 - j];
        }
        str = string(s);
    }
}

Here is the Browser Solidity screen showing the workings of this algorithm:

enter image description here

Martín Coll
  • 103
  • 4
BokkyPooBah
  • 40,274
  • 14
  • 123
  • 193
  • 3
    Is there a reason maxlength is 100 and not 78? The maximum number of digits possible with uint256 is 78. – mudgen Mar 13 '18 at 17:02
  • 2
    Doesn't work anymore.Remix throws an error saying explicit conversion from uint256 to bytes1 is unsupported for the line reversed[i++] = byte(48 + remainder) – Rahul Kothari Sep 08 '19 at 13:08
  • 2
    for conversion issue, use byte(uint8(48 + remainder)) – xuanzhui Jun 04 '21 at 10:22
  • This code is not working because type mismatch **reversed[i++] = byte(48 + remainder)** – Mahdi Mar 30 '23 at 03:56
-2
function strConcat(string _a, string _b, string _c, string _d, string _e) internal returns (string){
    bytes memory _ba = bytes(_a);
    bytes memory _bb = bytes(_b);
    bytes memory _bc = bytes(_c);
    bytes memory _bd = bytes(_d);
    bytes memory _be = bytes(_e);
    string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length);
    bytes memory babcde = bytes(abcde);
    uint k = 0;
    for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i];
    for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i];
    for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i];
    for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i];
    for (i = 0; i < _be.length; i++) babcde[k++] = _be[i];
    return string(babcde);
}

function strConcat(string _a, string _b, string _c, string _d) internal returns (string) {
    return strConcat(_a, _b, _c, _d, "");
}

function strConcat(string _a, string _b, string _c) internal returns (string) {
    return strConcat(_a, _b, _c, "", "");
}

function strConcat(string _a, string _b) internal returns (string) {
    return strConcat(_a, _b, "", "", "");
}