to be
a problem slover

solidity uint to string

pragma solidity 0.6.6;

contract UintToString{

    event toString(uint,string);

    function toStringTest(uint input) external {
        emit toString(input,uintToString(input));
    }

    function uintToString(uint v) pure internal returns (string memory) {
        // max uint 115792089237316195423570985008687907853269984665640564039457584007913129639935 has 78 digits
        uint maxlength = 78;
        bytes memory reversed = new bytes(maxlength);
        uint i = 0;
        while (v != 0) {
            uint remainder = v % 10;
            v = v / 10;
            reversed[i++] = byte(uint8(48 + remainder));
        }
        bytes memory s = new bytes(i);
        for (uint j = 0; j < i; j++) {
            s[j] = reversed[i - j - 1];
        }
        string memory str = string(s);
        return str;
    }
}
赞(0) 打赏
欢迎转载,注明出处:刘世明的博客 » solidity uint to string

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下作者

支付宝扫一扫打赏

微信扫一扫打赏