Skip to content

Commit

Permalink
Fix conversion from uint256 to string
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekorol committed Dec 18, 2020
1 parent f9173c7 commit d2ca110
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions contracts/utils/Strings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ library Strings {
digits++;
temp /= 10;
}
bytes memory buffer = new bytes(digits);
uint256 index = digits - 1;
temp = value;
while (temp != 0) {
buffer[index--] = bytes1(uint8(48 + temp % 10));
temp /= 10;
bytes memory reversed = new bytes(digits + 1);
uint256 i = 0;
while (value != 0) {
uint256 remainder = value % 10;
value /= 10;
reversed[i++] = bytes1(uint8(48 + remainder));
}

bytes memory buffer = new bytes(i);
for (uint256 j = 1; j <= i ; j++) {
buffer[j - 1] = reversed[i - j];
}

return string(buffer);
}
}

0 comments on commit d2ca110

Please sign in to comment.