Strings.sol 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. pragma solidity ^0.5.0;
  2. /**
  3. * @title Strings
  4. * @dev String operations.
  5. */
  6. library Strings {
  7. /**
  8. * Concatenates two strings.
  9. * string(abi.encodePacked(a, b))
  10. * https://solidity.readthedocs.io/en/latest/types.html?highlight=concatenate
  11. */
  12. /**
  13. * @dev Converts a uint256 to a string.
  14. * via OraclizeAPI - MIT licence
  15. * https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
  16. */
  17. function fromUint256(uint256 value) internal pure returns (string memory) {
  18. if (value == 0) {
  19. return "0";
  20. }
  21. uint256 temp = value;
  22. uint256 digits;
  23. while (temp != 0) {
  24. digits++;
  25. temp /= 10;
  26. }
  27. bytes memory buffer = new bytes(digits);
  28. uint256 index = digits - 1;
  29. temp = value;
  30. while (temp != 0) {
  31. buffer[index--] = byte(uint8(48 + temp % 10));
  32. temp /= 10;
  33. }
  34. return string(buffer);
  35. }
  36. }