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

1

3 Answers 3

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

4
  • 3
    Is there a reason maxlength is 100 and not 78? The maximum number of digits possible with uint256 is 78.
    – mudgen
    Commented Mar 13, 2018 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) Commented Sep 8, 2019 at 13:08
  • 2
    for conversion issue, use byte(uint8(48 + remainder))
    – xuanzhui
    Commented Jun 4, 2021 at 10:22
  • This code is not working because type mismatch ****reversed[i++] = byte(48 + remainder)****
    – Mahdi
    Commented Mar 30, 2023 at 3:56
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.

1
  • 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 Commented Dec 15, 2022 at 16:20
-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, "", "", "");
}
2
  • I don't think this answer addresses how to convert integer to a string Commented Dec 23, 2016 at 20:12
  • 1
    Isn't this just a copy of the answer from the suggested duplicate thread? In which case you should also flag the question as a duplicate. Commented Dec 24, 2016 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.