Class ByteUtil

java.lang.Object
com.github.tadukoo.util.ByteUtil

public final class ByteUtil extends Object
Util functions for dealing with bytes.
Version:
Beta v.0.5
Author:
Logan Ferree (Tadukoo)
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final char[]
    An array containing 0-9 and then A-F, used for converting to hex
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Not allowed to create a ByteUtil
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    checkBit(byte bite, int position)
    Checks if the bit at the given position is set or not
    static byte
    clearBit(byte bite, int position)
    Clears the bit at the given position (sets to 0/false)
    static byte[]
    Convert the given hex string into a byte array
    static int
    getBit(byte bite, int position)
    Gets the bit at the given position
    static int
    hexToInt(char hexChar)
    Converts the given hex character to an int
    static Byte
    parseByte(String byteString)
    Parse the given String into a byte.
    static byte
    setBit(byte bite, int position)
    Sets the bit at the given position (to 1/true)
    static String
    toBinaryString(byte bite)
    Parse the given byte into a binary string
    static byte
    toggleBit(byte bite, int position)
    Toggles the bit at the given position
    static String
    toHex(byte bite)
    Converts the given byte to a 2 digit hex string
    static String
    toHex(byte[] bytes)
    Converts the given byte array into a hex string
    static int
    toSignedInt(byte bite)
    Returns the byte as an int, signed (-128 to 127)
    static int
    toUnsignedInt(byte bite)
    Returns the byte as an unsigned int (0 to 255)

    Methods inherited from class java.lang.Object

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

    • hexChars

      public static final char[] hexChars
      An array containing 0-9 and then A-F, used for converting to hex
  • Constructor Details

    • ByteUtil

      private ByteUtil()
      Not allowed to create a ByteUtil
  • Method Details

    • getBit

      public static int getBit(byte bite, int position)
      Gets the bit at the given position
      Parameters:
      bite - The byte to grab a bit from
      position - The position of the bit to be grabbed
      Returns:
      The value of the bit at the given position
    • setBit

      public static byte setBit(byte bite, int position)
      Sets the bit at the given position (to 1/true)
      Parameters:
      bite - The byte to set the bit on
      position - The position of the bit to be set
      Returns:
      A byte with the given bit set
    • clearBit

      public static byte clearBit(byte bite, int position)
      Clears the bit at the given position (sets to 0/false)
      Parameters:
      bite - The byte to clear the bit on
      position - The position of the bit to be cleared
      Returns:
      A byte with the given bit cleared
    • toggleBit

      public static byte toggleBit(byte bite, int position)
      Toggles the bit at the given position
      Parameters:
      bite - The byte to toggle the bit on
      position - The position of the bit to be toggled
      Returns:
      A byte with the given bit toggled
    • checkBit

      public static boolean checkBit(byte bite, int position)
      Checks if the bit at the given position is set or not
      Parameters:
      bite - The byte to check a bit from
      position - The position of the bit to be checked
      Returns:
      true if the bit is set (equal to 1) or false if not set (equal to 0)
    • toBinaryString

      public static String toBinaryString(byte bite)
      Parse the given byte into a binary string
      Parameters:
      bite - The byte to be converted to a binary string
      Returns:
      The binary string representation of the byte
    • parseByte

      public static Byte parseByte(String byteString)
      Parse the given String into a byte. String should be a binary representation of a byte
      Parameters:
      byteString - The binary representation of a byte
      Returns:
      A Byte parsed from the given binary String
    • toSignedInt

      public static int toSignedInt(byte bite)
      Returns the byte as an int, signed (-128 to 127)
      Parameters:
      bite - The byte to convert to an int
      Returns:
      The int value of the given byte
    • toUnsignedInt

      public static int toUnsignedInt(byte bite)
      Returns the byte as an unsigned int (0 to 255)
      Parameters:
      bite - The byte to convert to an int
      Returns:
      The int value of the given byte
    • toHex

      public static String toHex(byte bite)
      Converts the given byte to a 2 digit hex string
      Parameters:
      bite - The byte to convert to hex
      Returns:
      The hex representation of the given byte
    • toHex

      public static String toHex(byte[] bytes)
      Converts the given byte array into a hex string
      Parameters:
      bytes - The bytes to convert to hex
      Returns:
      The hex representation of the given bytes
    • hexToInt

      public static int hexToInt(char hexChar)
      Converts the given hex character to an int
      Parameters:
      hexChar - The hex character to convert
      Returns:
      The int value of the hex character, or -1 if invalid
    • fromHex

      public static byte[] fromHex(String hex)
      Convert the given hex string into a byte array
      Parameters:
      hex - The hex string to convert
      Returns:
      A byte array representing the given hex string