# Solidity concatenate uint into a string?

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`

## 2 Answers

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:

• Is there a reason maxlength is 100 and not 78? The maximum number of digits possible with uint256 is 78. Mar 13, 2018 at 17:02
• 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)` Sep 8, 2019 at 13:08
• for conversion issue, use `byte(uint8(48 + remainder))` Jun 4, 2021 at 10:22
``````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, "", "", "");
}
``````
• I don't think this answer addresses how to convert integer to a string Dec 23, 2016 at 20:12
• 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. Dec 24, 2016 at 9:01