Class BitConverter

java.lang.Object
com.mku.convert.BitConverter

public class BitConverter extends Object
Converts from/to: byte arrays, integral values, and hex strings.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    toBytes(long value, int length)
    Converts a long value to byte array.
    static byte[]
    Convert a hex string to a byte array.
    static String
    toHex(byte[] data)
    Convert a byte array to a hex representation.
    static long
    toLong(byte[] bytes, int index, int length)
    Converts a byte array to a long value.

    Methods inherited from class java.lang.Object

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

    • BitConverter

      public BitConverter()
  • Method Details

    • toBytes

      public static byte[] toBytes(long value, int length)
      Converts a long value to byte array.
      Parameters:
      value - The value to be converted.
      length - The length of the byte array to be returned.
      Returns:
      A byte array representation of the value.
    • toLong

      public static long toLong(byte[] bytes, int index, int length)
      Converts a byte array to a long value. Little endian only.
      Parameters:
      bytes - The byte array to be converted.
      index - The starting index of the data in the array that will be converted.
      length - The length of the data that will be converted.
      Returns:
      The long value representation of the byte array.
    • toHex

      public static String toHex(byte[] data)
      Convert a byte array to a hex representation.
      Parameters:
      data - The byte array to be converted.
      Returns:
      The hex string representation.
    • toBytes

      public static byte[] toBytes(String data)
      Convert a hex string to a byte array.
      Parameters:
      data - The hex string to be converted.
      Returns:
      The byte array converted from the string.