Class Converter

java.lang.Object
org.symbol.sdk.utils.Converter

public final class Converter extends Object
Converter

Hex / integer / byte conversion helpers.

  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    bytesToInt(byte[] input, int size)
    Converts little-endian bytes to a long for any size (1, 2, 4 or 8); a size-8 u64 >= 2^63 reads back negative.
    static long
    bytesToInt(byte[] input, int size, boolean isSigned)
    Converts bytes to a long for any size (1, 2, 4 or 8); a size-8 u64 >= 2^63 reads back negative.
    static long
    bytesToInt(byte[] input, int offset, int size, boolean isSigned)
    Converts bytes to a long for any size (1, 2, 4 or 8); a size-8 u64 >= 2^63 reads back negative.
    static byte[]
    Converts a hex string to a byte array.
    static byte[]
    intToBytes(long value, int byteSize)
    Converts an integer to bytes by truncating its 64-bit two's-complement encoding to byteSize.
    static boolean
    Determines whether a string is a hex string.
    static int
    toInt(Number value)
    Converts a fixed-width integral wrapper to a 32-bit int bit pattern, accepting the signed int range and the full unsigned 32-bit range (a magnitude >= 2^31 yields the negative two's-complement pattern).
    static int
    toInt(String value)
    Parses a string to a 32-bit int bit pattern, the 32-bit analogue of toLong(String): a 0x-prefixed hex string or a non-negative decimal is read across the full unsigned 32-bit range (a value >= 2^31 yields the negative two's-complement pattern), while a --prefixed decimal is parsed signed.
    static long
    toLong(Number value)
    Converts a fixed-width integral wrapper (Integer/Long/Short/Byte) or an unsigned-64-bit-range BigInteger to a 64-bit long; any other type is rejected.
    static long
    toLong(String value)
    Parses a string to a 64-bit long bit pattern: a 0x/0X-prefixed hex string or a non-negative decimal is read across the full unsigned range (a value >= 2^63 yields the negative two's-complement pattern), while a --prefixed decimal is parsed signed — so, like toLong(Number), negatives are accepted.
    static String
    uint8ToHex(byte[] input)
    Converts a byte array to a hex string.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isHexString

      public static boolean isHexString(String input)
      Converter.isHexString

      Determines whether a string is a hex string.

      Parameters:

      input - String to test.

      Returns:

      true if the input is a hex string, false otherwise.

    • hexToUint8

      public static byte[] hexToUint8(String input)
      Converter.hexToUint8

      Converts a hex string to a byte array.

      Parameters:

      input - Hex encoded string.

      Returns:

      Byte array corresponding to the input.

    • uint8ToHex

      public static String uint8ToHex(byte[] input)
      Converter.uint8ToHex

      Converts a byte array to a hex string.

      Parameters:

      input - Byte array.

      Returns:

      Hex encoded string corresponding to the input.

    • bytesToInt

      public static long bytesToInt(byte[] input, int size, boolean isSigned)
      Converter.bytesToInt

      Converts bytes to a long for any size (1, 2, 4 or 8); a size-8 u64 >= 2^63 reads back negative.

      Parameters:

      input - Byte array.

      size - Number of bytes (1, 2, 4 or 8).

      isSigned - true if a value narrower than 8 bytes should be sign-extended.

      Returns:

      Value corresponding to the input.

    • bytesToInt

      public static long bytesToInt(byte[] input, int offset, int size, boolean isSigned)
      Converter.bytesToInt

      Converts bytes to a long for any size (1, 2, 4 or 8); a size-8 u64 >= 2^63 reads back negative.

      Parameters:

      input - Byte array.

      offset - Offset into the array at which to start reading.

      size - Number of bytes (1, 2, 4 or 8).

      isSigned - true if a value narrower than 8 bytes should be sign-extended.

      Returns:

      Value corresponding to the input.

    • bytesToInt

      public static long bytesToInt(byte[] input, int size)
      Converter.bytesToInt

      Converts little-endian bytes to a long for any size (1, 2, 4 or 8); a size-8 u64 >= 2^63 reads back negative.

      Parameters:

      input - Byte array.

      size - Number of bytes (1, 2, 4 or 8).

      Returns:

      Unsigned value.

    • intToBytes

      public static byte[] intToBytes(long value, int byteSize)
      Converter.intToBytes

      Converts an integer to bytes by truncating its 64-bit two's-complement encoding to byteSize.

      Parameters:

      value - Integer value.

      byteSize - Number of output bytes.

      Returns:

      Byte representation of the integer.

    • toLong

      public static long toLong(String value)
      Converter.toLong

      Parses a string to a 64-bit long bit pattern: a 0x/0X-prefixed hex string or a non-negative decimal is read across the full unsigned range (a value >= 2^63 yields the negative two's-complement pattern), while a --prefixed decimal is parsed signed — so, like toLong(Number), negatives are accepted.

      Parameters:

      value - String value (signed/unsigned decimal, or 0x-prefixed hexadecimal).

      Returns:

      Parsed value as a 64-bit two's-complement bit pattern.

      Throws:

      IllegalArgumentException - If the string is not a valid integer that fits a 64-bit pattern.

    • toLong

      public static long toLong(Number value)
      Converter.toLong

      Converts a fixed-width integral wrapper (Integer/Long/Short/Byte) or an unsigned-64-bit-range BigInteger to a 64-bit long; any other type is rejected.

      Parameters:

      value - Integral descriptor number.

      Returns:

      Converted value as a 64-bit two's-complement bit pattern.

      Throws:

      IllegalArgumentException - If the value is not an integral number fitting a 64-bit pattern.

    • toInt

      public static int toInt(Number value)
      Converter.toInt

      Converts a fixed-width integral wrapper to a 32-bit int bit pattern, accepting the signed int range and the full unsigned 32-bit range (a magnitude >= 2^31 yields the negative two's-complement pattern). Anything outside [Integer.MIN_VALUE, 2^32 - 1] is rejected rather than truncated.

      Parameters:

      value - Integral descriptor number.

      Returns:

      Converted value as a 32-bit two's-complement bit pattern.

      Throws:

      IllegalArgumentException - If the value does not fit a 32-bit pattern (or is not an integral wrapper).

    • toInt

      public static int toInt(String value)
      Converter.toInt

      Parses a string to a 32-bit int bit pattern, the 32-bit analogue of toLong(String): a 0x-prefixed hex string or a non-negative decimal is read across the full unsigned 32-bit range (a value >= 2^31 yields the negative two's-complement pattern), while a --prefixed decimal is parsed signed. Anything outside the unsigned 32-bit range is rejected rather than truncated.

      Parameters:

      value - String value (signed/unsigned decimal, or 0x-prefixed hexadecimal).

      Returns:

      Parsed value as a 32-bit two's-complement bit pattern.

      Throws:

      IllegalArgumentException - If the string is unparseable or does not fit a 32-bit pattern.