0

I have a JSON string, a part of which looks like this:

\"min\":1,\"max\":4,\

I want to make the values 1 and 4 dynamic, by adding the variables min and max, but I'm struggling with the syntax of how to do that. What would be the best way of doing that in Solidity?

1
pragma solidity ^0.4.23;

contract s {
    uint private _min = 0;
    uint private _max = 2;

    function go() public view returns(string) {
        return string(abi.encodePacked("\"min\":", uint2str(_min), ",\"max\":", uint2str(_max), ","));
    }

    function uint2str(uint i) internal pure returns (string){
        if (i == 0) return "0";
        uint j = i;
        uint length;
        while (j != 0){
            length++;
            j /= 10;
        }
        bytes memory bstr = new bytes(length);
        uint k = length - 1;
        while (i != 0){
            bstr[k--] = byte(48 + i % 10);
            i /= 10;
        }
        return string(bstr);
    }
}
| improve this answer | |

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.