Class HexString

A util class for working with hex strings. Hex strings are strings that are prefixed with 0x

Constructors

  • Creates new HexString instance from regular string. If specified string already starts with "0x" prefix, it will not add another one

    Parameters

    • hexString: string

      String to convert

    Returns HexString

    Example

     const string = "string";
    new HexString(string); // "0xstring"

Properties

hexString: any

Methods

  • Getter for inner hexString

    Returns string

    Inner hex string

  • Getter for inner hexString without prefix

    Returns string

    Inner hex string without prefix

    Example

     const hexString = new HexString("string"); // "0xstring"
    hexString.noPrefix(); // "string"
  • Trimmes extra zeroes in the begining of a string

    Returns string

    Inner hexString without leading zeroes

    Example

     new HexString("0x000000string").toShortString(); // result = "0xstring"
    
  • Overrides default toString method

    Returns string

    Inner hex string

  • Converts hex string to a Uint8Array

    Returns Uint8Array

    Uint8Array from inner hexString without prefix

  • Ensures hexString is instance of HexString class

    Parameters

    • hexString: MaybeHexString

      String to check

    Returns HexString

    New HexString if hexString is regular string or hexString if it is HexString instance

    Example

     const regularString = "string";
    const hexString = new HexString("string"); // "0xstring"
    HexString.ensure(regularString); // "0xstring"
    HexString.ensure(hexString); // "0xstring"
  • Creates new hex string from Buffer

    Parameters

    • buffer: Uint8Array

      A buffer to convert

    Returns HexString

    New HexString

  • Creates new hex string from Uint8Array

    Parameters

    • arr: Uint8Array

      Uint8Array to convert

    Returns HexString

    New HexString