0

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;

}
1

In your version of the code, you're just overwriting reversed, so all maxlength is doing is setting how long reversed is before you call changeBytes.

In the function you reference, reversed is maxlength bytes long and never overwritten. (Individual bytes are set, but the array is the one defined at the top.)

  • I'm still a bit unclear. If "reversed" isn't over-written in the function, wouldn't it just be an empty value? And if it can be over-written with an arbitrary number of bytes, it doesn't force any limits? What does the line "new bytes(maxlength)" actually do to "reversed"? – CreatedAMadman Jun 14 '18 at 3:17
  • As I said, individual bytes are set. bytes memory reversed = new bytes(100) creates an array of bytes that's 100 elements long. Then you can do things like reversed[0] = 1 to store a 1 in the first byte, etc. – user19510 Jun 14 '18 at 3:19
  • Ah, okay. I knew that "bytes" was an array, but hadn't yet realized you can call upon individual bytes with index numbers as you can in other arrays - instead of just rewriting the whole thing at once. – CreatedAMadman Jun 14 '18 at 15:27
1

It's the maximum number of digits it's expecting the integer to have. Since you have to initialise the byte array with the length, you need to specify this ahead of time.

But actually, the maximum value of a uint is 115792089237316195423570985008687907853269984665640564039457584007913129639935, which is only 78 digits long. So you should probably set maxLength to 78 and save yourself some gas.

  • Ah yes, I up-voted the comment on the original post because I was curious about that too. Thanks for the confirmation. – CreatedAMadman Jun 14 '18 at 15:28
  • Actually, I'm a bit unclear about this. This is the largest 256 bit or 32 byte integer. Shouldn't maxLength be 32 since it pertains to bytes? – CreatedAMadman Jun 14 '18 at 22:51
  • Nah, because we aren't talking about bytes or bits (ie, how it's stored), we're talking about how many actual digits it has when it's represented as a base-10 number (which is how people generally read numbers). – AnAllergyToAnalogy Jun 14 '18 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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