I am trying to understand the function "appendUintToString" from Solidity concatenate uint into a string? The function is recreated below. For now, I am trying to understand what the integer "maxlength" does.
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);
}
When I isolate it in Remix with the following code, the getter for "reversed" returns an empty 0 slot, but the numbers in the slot match the maxlength integer. So, when maxlength = 20, returnBytes returns 0x0000000000000000000000000000000000000000, and if maxlength = 2, it returns 0x0000, and so on.
This suggests that the maxlength integer provides a limit for how many bytes can be represented with "reversed". Yet, when I set "reversed" with the changeBytes function, there seems to be no limit. If maxlength = 2, for example, it will allow me to input three or more in this function and the getter function will then return three or more (it doesn't cut off the extra). So then what is the point of maxvalue?
uint public maxlength = 20;
bytes reversed = new bytes(maxlength);
function changeBytes(bytes _bytes) public {
reversed = _bytes;
}
function returnBytes() public view returns(bytes) {
return reversed;
}