Strings.sol 938 B

12345678910111213141516171819202122232425262728293031323334
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.6.0 <0.8.0;
  3. /**
  4. * @dev String operations.
  5. */
  6. library Strings {
  7. /**
  8. * @dev Converts a `uint256` to its ASCII `string` representation.
  9. */
  10. function toString(uint256 value) internal pure returns (string memory) {
  11. // Inspired by OraclizeAPI's implementation - MIT licence
  12. // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
  13. if (value == 0) {
  14. return "0";
  15. }
  16. uint256 temp = value;
  17. uint256 digits;
  18. while (temp != 0) {
  19. digits++;
  20. temp /= 10;
  21. }
  22. bytes memory buffer = new bytes(digits);
  23. uint256 index = digits - 1;
  24. temp = value;
  25. while (temp != 0) {
  26. buffer[index--] = bytes1(uint8(48 + temp % 10));
  27. temp /= 10;
  28. }
  29. return string(buffer);
  30. }
  31. }