Package ghidra.util

# Interface DataConverter

All Superinterfaces:
  `Serializable`

All Known Subinterfaces:
  `GhidraDataConverter`

All Known Implementing Classes:
  `BigEndianDataConverter`, `GhidraBigEndianDataConverter`, `GhidraLittleEndianDataConverter`, `LittleEndianDataConverter`

public interface DataConverter extends Serializable
Stateless helper classes with static singleton instances that contain methods to convert Java numeric types to and from their raw form in a byte array.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `default BigInteger`
  `getBigInteger(byte[] b, int size, boolean signed)`
  Get the value from the given byte array using the specified size.
  `BigInteger`
  `getBigInteger(byte[] b, int offset, int size, boolean signed)`
  Get the value from the given byte array using the specified size.
  `default byte[]`
  `getBytes(int value)`
  Converts the int value to an array of bytes.
  `default void`
  `getBytes(int value, byte[] b)`
  Converts the given value to bytes.
  `default void`
  `getBytes(int value, byte[] b, int offset)`
  Converts the given value to bytes.
  `default byte[]`
  `getBytes(long value)`
  Converts the long value to an array of bytes.
  `default void`
  `getBytes(long value, byte[] b)`
  Converts the given value to bytes.
  `default void`
  `getBytes(long value, byte[] b, int offset)`
  Converts the given value to bytes.
  `default void`
  `getBytes(long value, int size, byte[] b, int offset)`
  Converts the given value to bytes using the number of least significant bytes specified by size.
  `default byte[]`
  `getBytes(short value)`
  Converts the short value to an array of bytes.
  `default void`
  `getBytes(short value, byte[] b)`
  Converts the given value to bytes.
  `default void`
  `getBytes(short value, byte[] b, int offset)`
  Converts the given value to bytes.
  `default byte[]`
  `getBytes(BigInteger value, int size)`
  Converts the value to an array of bytes.
  `default void`
  `getBytes(BigInteger value, int size, byte[] b, int offset)`
  Converts the given value to bytes using the number of least significant bytes specified by size.
  `static DataConverter`
  `getInstance(boolean isBigEndian)`
  Returns the correct DataConverter static instance for the requested endian-ness.
  `default int`
  `getInt(byte[] b)`
  Get the int value from the given byte array.
  `int`
  `getInt(byte[] b, int offset)`
  Get the int value from the given byte array.
  `default long`
  `getLong(byte[] b)`
  Get the long value from the given byte array.
  `long`
  `getLong(byte[] b, int offset)`
  Get the long value from the given byte array.
  `default short`
  `getShort(byte[] b)`
  Get the short value from the given byte array.
  `short`
  `getShort(byte[] b, int offset)`
  Get the short value from the given byte array.
  `default long`
  `getSignedValue(byte[] b, int size)`
  Get the signed value from the given byte array using the specified integer size, returned as a long.
  `default long`
  `getSignedValue(byte[] b, int offset, int size)`
  Get the signed value from the given byte array using the specified integer size, returned as a long.
  `default long`
  `getValue(byte[] b, int size)`
  Get the unsigned value from the given byte array using the specified integer size, returned as a long.
  `long`
  `getValue(byte[] b, int offset, int size)`
  Get the unsigned value from the given byte array using the specified integer size, returned as a long.
  `default boolean`
  `isBigEndian()`
  Returns the endianness of this DataConverter instance.
  `void`
  `putBigInteger(byte[] b, int offset, int size, BigInteger value)`
  Writes a value of specified size into the byte array at the given offset
  `default void`
  `putBigInteger(byte[] b, int size, BigInteger value)`
  Writes a value of specified size into the byte array at the given offset.
  `default void`
  `putInt(byte[] b, int value)`
  Writes a int value into a byte array.
  `void`
  `putInt(byte[] b, int offset, int value)`
  Writes a int value into the byte array at the given offset.
  `default void`
  `putLong(byte[] b, int offset, long value)`
  Writes a long value into the byte array at the given offset
  `default void`
  `putLong(byte[] b, long value)`
  Writes a long value into a byte array.
  `void`
  `putShort(byte[] b, int offset, short value)`
  Writes a short value into the byte array at the given offset
  `default void`
  `putShort(byte[] b, short value)`
  Writes a short value into a byte array.
  `void`
  `putValue(long value, int size, byte[] b, int offset)`
  Converts the given value to bytes using the number of least significant bytes specified by size.
  `static long`
  `swapBytes(long val, int size)`
  Swap the least-significant bytes (based upon size)

- ## Method Details

  - ### getInstance

    static DataConverter getInstance(boolean isBigEndian)
    Returns the correct DataConverter static instance for the requested endian-ness.
    Parameters:
      `isBigEndian` - boolean flag, true means big endian
    Returns:
      static DataConverter instance

  - ### isBigEndian

    default boolean isBigEndian()
    Returns the endianness of this DataConverter instance.
    Returns:
      boolean flag, true means big-endian

  - ### getShort

    default short getShort(byte[] b)
    Get the short value from the given byte array.
    Parameters:
      `b` - array containing bytes
    Returns:
      signed short value from the beginning of the specified array
    Throws:
      `IndexOutOfBoundsException` - if byte array size is less than 2.

  - ### getShort

    short getShort(byte[] b, int offset)
    Get the short value from the given byte array.
    Parameters:
      `b` - array containing bytes
      `offset` - offset into byte array for getting the short
    Returns:
      signed short value
    Throws:
      `IndexOutOfBoundsException` - if byte array size is less than offset+2

  - ### getInt

    default int getInt(byte[] b)
    Get the int value from the given byte array.
    Parameters:
      `b` - array containing bytes
    Returns:
      signed int value from the beginning of the specified array
    Throws:
      `IndexOutOfBoundsException` - if byte array size is less than 4

  - ### getInt

    int getInt(byte[] b, int offset)
    Get the int value from the given byte array.
    Parameters:
      `b` - array containing bytes
      `offset` - offset into byte array for getting the int
    Returns:
      signed int value
    Throws:
      `IndexOutOfBoundsException` - if byte array size is less than offset+4

  - ### getLong

    default long getLong(byte[] b)
    Get the long value from the given byte array.
    Parameters:
      `b` - array containing bytes
    Returns:
      signed long value from the beginning of the specified array
    Throws:
      `IndexOutOfBoundsException` - if byte array size is less than 8

  - ### getLong

    long getLong(byte[] b, int offset)
    Get the long value from the given byte array.
    Parameters:
      `b` - array containing bytes
      `offset` - offset into byte array for getting the long
    Returns:
      signed long value
    Throws:
      `IndexOutOfBoundsException` - if byte array size is less than offset+8

  - ### getValue

    default long getValue(byte[] b, int size)
    Get the unsigned value from the given byte array using the specified integer size, returned as a long.

    Values with a size less than sizeof(long) will not have their sign bit extended and therefore will appear as an 'unsigned' value.

    Casting the 'unsigned' long value to the correctly sized smaller java primitive will cause the value to appear as a signed value.

    Values of size 8 (ie. longs) will be signed.

    Parameters:
      `b` - array containing bytes
      `size` - number of bytes (1 - 8) to use from array at offset 0
    Returns:
      unsigned value from the beginning of the specified array
    Throws:
      `IndexOutOfBoundsException` - if byte array size is less than specified size

  - ### getValue

    long getValue(byte[] b, int offset, int size)
    Get the unsigned value from the given byte array using the specified integer size, returned as a long.

    Values with a size less than sizeof(long) will not have their sign bit extended and therefore will appear as an 'unsigned' value.

    Casting the 'unsigned' long value to the correctly sized smaller java primitive will cause the value to appear as a signed value.

    Values of size 8 (ie. longs) will be signed.

    Parameters:
      `b` - array containing bytes
      `offset` - offset into byte array for getting the long
      `size` - number of bytes (1 - 8) to use from array
    Returns:
      unsigned value
    Throws:
      `IndexOutOfBoundsException` - if byte array size is less than offset+size or size is greater than 8 (sizeof long)

  - ### getSignedValue

    default long getSignedValue(byte[] b, int size)
    Get the signed value from the given byte array using the specified integer size, returned as a long.

    Values with a size less than sizeof(long) will have their sign bit extended.

    Parameters:
      `b` - array containing bytes
      `size` - number of bytes (1 - 8) to use from array at offset 0
    Returns:
      signed value from the beginning of the specified array
    Throws:
      `IndexOutOfBoundsException` - if byte array size is less than specified size

  - ### getSignedValue

    default long getSignedValue(byte[] b, int offset, int size)
    Get the signed value from the given byte array using the specified integer size, returned as a long.

    Values with a size less than sizeof(long) will have their sign bit extended.

    Parameters:
      `b` - array containing bytes
      `offset` - offset into byte array for getting the long
      `size` - number of bytes (1 - 8) to use from array
    Returns:
      signed value
    Throws:
      `IndexOutOfBoundsException` - if byte array size is less than offset+size or size is greater than 8 (sizeof long)

  - ### getBigInteger

    default BigInteger getBigInteger(byte[] b, int size, boolean signed)
    Get the value from the given byte array using the specified size.
    Parameters:
      `b` - array containing bytes
      `size` - number of bytes to use from array at offset 0
      `signed` - boolean flag indicating the value is signed
    Returns:
      `BigInteger` with value
    Throws:
      `IndexOutOfBoundsException` - if byte array size is less than size

  - ### getBigInteger

    BigInteger getBigInteger(byte[] b, int offset, int size, boolean signed)
    Get the value from the given byte array using the specified size.
    Parameters:
      `b` - array containing bytes
      `offset` - offset into byte array for getting the long
      `size` - number of bytes to use from array
      `signed` - boolean flag indicating the value is signed
    Returns:
      `BigInteger` with value
    Throws:
      `IndexOutOfBoundsException` - if byte array size is less than offset+size

  - ### getBytes

    default byte[] getBytes(short value)
    Converts the short value to an array of bytes.
    Parameters:
      `value` - short value to be converted
    Returns:
      array of bytes

  - ### getBytes

    default byte[] getBytes(int value)
    Converts the int value to an array of bytes.
    Parameters:
      `value` - int value to be converted
    Returns:
      array of bytes

  - ### getBytes

    default byte[] getBytes(long value)
    Converts the long value to an array of bytes.
    Parameters:
      `value` - long value to be converted
    Returns:
      array of bytes

  - ### getBytes

    default byte[] getBytes(BigInteger value, int size)
    Converts the value to an array of bytes.
    Parameters:
      `value` - value to be converted
      `size` - value size in bytes
    Returns:
      array of bytes

  - ### putShort

    default void putShort(byte[] b, short value)
    Writes a short value into a byte array.
    Parameters:
      `b` - array to contain the bytes
      `value` - the short value
    Throws:
      `IndexOutOfBoundsException` - if byte array is too small to hold the value

  - ### putShort

    void putShort(byte[] b, int offset, short value)
    Writes a short value into the byte array at the given offset
    Parameters:
      `b` - array to contain the bytes
      `offset` - the offset into the byte array to store the value
      `value` - the short value
    Throws:
      `IndexOutOfBoundsException` - if offset is too large or byte array is too small to hold the value

  - ### putInt

    default void putInt(byte[] b, int value)
    Writes a int value into a byte array.

    See `getBytes(int, byte[])`

    Parameters:
      `b` - array to contain the bytes
      `value` - the int value
    Throws:
      `IndexOutOfBoundsException` - if byte array is too small to hold the value

  - ### putInt

    void putInt(byte[] b, int offset, int value)
    Writes a int value into the byte array at the given offset.

    See `getBytes(int, byte[], int)`

    Parameters:
      `b` - array to contain the bytes
      `offset` - the offset into the byte array to store the value
      `value` - the int value
    Throws:
      `IndexOutOfBoundsException` - if offset is too large or byte array is too small to hold the value

  - ### putLong

    default void putLong(byte[] b, long value)
    Writes a long value into a byte array.

    See `getBytes(long, byte[])`

    Parameters:
      `b` - array to contain the bytes
      `value` - the long value
    Throws:
      `IndexOutOfBoundsException` - if byte array is too small to hold the value

  - ### putLong

    default void putLong(byte[] b, int offset, long value)
    Writes a long value into the byte array at the given offset

    See `getBytes(long, byte[], int)`

    Parameters:
      `b` - array to contain the bytes
      `offset` - the offset into the byte array to store the value
      `value` - the long value
    Throws:
      `IndexOutOfBoundsException` - if offset is too large or byte array is too small to hold the value

  - ### putValue

    void putValue(long value, int size, byte[] b, int offset)
    Converts the given value to bytes using the number of least significant bytes specified by size.
    Parameters:
      `value` - value to convert to bytes
      `size` - number of least significant bytes of value to be written to the byte array
      `b` - byte array to store bytes
      `offset` - offset into byte array to put the bytes
    Throws:
      `IndexOutOfBoundsException` - if (offset+size)>b.length

  - ### putBigInteger

    default void putBigInteger(byte[] b, int size, BigInteger value)
    Writes a value of specified size into the byte array at the given offset.

    See `getBytes(BigInteger, int, byte[], int)`

    Parameters:
      `b` - array to contain the bytes at offset 0
      `size` - number of bytes to be written
      `value` - BigInteger value to convert
    Throws:
      `IndexOutOfBoundsException` - if byte array is less than specified size

  - ### putBigInteger

    void putBigInteger(byte[] b, int offset, int size, BigInteger value)
    Writes a value of specified size into the byte array at the given offset

    See `getBytes(BigInteger, int, byte[], int)`

    Parameters:
      `b` - array to contain the bytes
      `offset` - the offset into the byte array to store the value
      `size` - number of bytes to be written
      `value` - BigInteger value to convert
    Throws:
      `IndexOutOfBoundsException` - if (offset+size)>b.length

  - ### getBytes

    default void getBytes(short value, byte[] b)
    Converts the given value to bytes. See `putShort(byte[], short)`
    Parameters:
      `value` - value to convert to bytes
      `b` - byte array to store bytes
    Throws:
      `IndexOutOfBoundsException` - if b.length is not at least 2.

  - ### getBytes

    default void getBytes(short value, byte[] b, int offset)
    Converts the given value to bytes.

    See `putShort(byte[], int, short)`

    Parameters:
      `value` - value to convert to bytes
      `b` - byte array to store bytes
      `offset` - offset into byte array to put the bytes
    Throws:
      `IndexOutOfBoundsException` - if (offset+2)>b.length

  - ### getBytes

    default void getBytes(int value, byte[] b)
    Converts the given value to bytes.

    See `putInt(byte[], int)`

    Parameters:
      `value` - value to convert to bytes
      `b` - byte array to store bytes
    Throws:
      `IndexOutOfBoundsException` - if b.length is not at least 4.

  - ### getBytes

    default void getBytes(int value, byte[] b, int offset)
    Converts the given value to bytes.

    See `putInt(byte[], int)`

    Parameters:
      `value` - value to convert to bytes
      `b` - byte array to store bytes
      `offset` - offset into byte array to put the bytes
    Throws:
      `IndexOutOfBoundsException` - if (offset+4)>b.length

  - ### getBytes

    default void getBytes(long value, byte[] b)
    Converts the given value to bytes.

    See `putLong(byte[], long)`

    Parameters:
      `value` - value to convert to bytes
      `b` - byte array to store bytes
    Throws:
      `IndexOutOfBoundsException` - if b.length is not at least 8.

  - ### getBytes

    default void getBytes(long value, byte[] b, int offset)
    Converts the given value to bytes.

    See `putLong(byte[], long)`

    Parameters:
      `value` - value to convert to bytes
      `b` - byte array to store bytes
      `offset` - offset into byte array to put the bytes
    Throws:
      `IndexOutOfBoundsException` - if (offset+8)>b.length

  - ### getBytes

    default void getBytes(long value, int size, byte[] b, int offset)
    Converts the given value to bytes using the number of least significant bytes specified by size.

    See `putValue(long, int, byte[], int)`

    Parameters:
      `value` - value to convert to bytes
      `size` - number of least significant bytes of value to be written to the byte array
      `b` - byte array to store bytes
      `offset` - offset into byte array to put the bytes
    Throws:
      `IndexOutOfBoundsException` - if (offset+size)>b.length

  - ### getBytes

    default void getBytes(BigInteger value, int size, byte[] b, int offset)
    Converts the given value to bytes using the number of least significant bytes specified by size.

    See `putBigInteger(byte[], int, BigInteger)`

    Parameters:
      `value` - value to convert to bytes
      `size` - number of least significant bytes of value to be written to the byte array
      `b` - byte array to store bytes
      `offset` - offset into byte array to put the bytes
    Throws:
      `IndexOutOfBoundsException` - if (offset+size)>b.length.

  - ### swapBytes

    static long swapBytes(long val, int size)
    Swap the least-significant bytes (based upon size)
    Parameters:
      `val` - value whose bytes are to be swapped
      `size` - number of least significant bytes to be swapped
    Returns:
      value with bytes swapped (any high-order bytes beyond size will be 0)