Strings.sol 895 B

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