Skip to content

Latest commit

 

History

History
289 lines (257 loc) · 8.57 KB

Strings.md

File metadata and controls

289 lines (257 loc) · 8.57 KB

Strings.sol

View Source: openzeppelin-solidity/contracts/utils/Strings.sol

Strings

String operations.

Contract Members

Constants & Variables

bytes16 private constant _HEX_SYMBOLS;

Functions

toString

Converts a uint256 to its ASCII string decimal representation.

function toString(uint256 value) internal pure
returns(string)

Arguments

Name Type Description
value uint256
Source Code
function toString(uint256 value) internal pure returns (string memory) {
        // Inspired by OraclizeAPI's implementation - MIT licence
        // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol

        if (value == 0) {
            return "0";
        }
        uint256 temp = value;
        uint256 digits;
        while (temp != 0) {
            digits++;
            temp /= 10;
        }
        bytes memory buffer = new bytes(digits);
        while (value != 0) {
            digits -= 1;
            buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
            value /= 10;
        }
        return string(buffer);
    }

toHexString

Converts a uint256 to its ASCII string hexadecimal representation.

function toHexString(uint256 value) internal pure
returns(string)

Arguments

Name Type Description
value uint256
Source Code
function toHexString(uint256 value) internal pure returns (string memory) {
        if (value == 0) {
            return "0x00";
        }
        uint256 temp = value;
        uint256 length = 0;
        while (temp != 0) {
            length++;
            temp >>= 8;
        }
        return toHexString(value, length);
    }

toHexString

Converts a uint256 to its ASCII string hexadecimal representation with fixed length.

function toHexString(uint256 value, uint256 length) internal pure
returns(string)

Arguments

Name Type Description
value uint256
length uint256
Source Code
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
        bytes memory buffer = new bytes(2 * length + 2);
        buffer[0] = "0";
        buffer[1] = "x";
        for (uint256 i = 2 * length + 1; i > 1; --i) {
            buffer[i] = _HEX_SYMBOLS[value & 0xf];
            value >>= 4;
        }
        require(value == 0, "Strings: hex length insufficient");
        return string(buffer);
    }

Contracts