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;
}
}
solidity uint to string
欢迎转载,注明出处:刘世明的博客 » solidity uint to string
最新评论