Package edu.rit.util

Class Hex

java.lang.Object
edu.rit.util.Hex

public class Hex extends Object
Class Hex provides static methods for converting between hexadecimal strings and numbers of various kinds.
Version:
22-Apr-2006
Author:
Alan Kaminsky
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte
    Convert the given hexadecimal string to a byte value.
    static byte[]
    Convert the given hexadecimal string to a sequence of bytes stored in a new byte array.
    static void
    toByteArray(String str, byte[] val)
    Convert the given hexadecimal string to a sequence of bytes stored in the given byte array.
    static void
    toByteArray(String str, byte[] val, int off, int len)
    Convert the given hexadecimal string to a sequence of bytes stored in a portion of the given byte array.
    static char
    Convert the given hexadecimal string to a char value.
    static int
    Convert the given hexadecimal string to an int value.
    static long
    Convert the given hexadecimal string to a long value.
    static short
    Convert the given hexadecimal string to a short value.
    static String
    toString(byte val)
    Convert the given byte value to a two-digit hexadecimal string.
    static String
    toString(byte[] val)
    Convert the given byte array to a hexadecimal string.
    static String
    toString(byte[] val, int off, int len)
    Convert a portion of the given byte array to a hexadecimal string.
    static String
    toString(char val)
    Convert the given char value to a four-digit hexadecimal string.
    static String
    toString(int val)
    Convert the given int value to an eight-digit hexadecimal string.
    static String
    toString(long val)
    Convert the given long value to a sixteen-digit hexadecimal string.
    static String
    toString(short val)
    Convert the given short value to a four-digit hexadecimal string.

    Methods inherited from class java.lang.Object

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

    • toString

      public static String toString(byte val)
      Convert the given byte value to a two-digit hexadecimal string.
      Parameters:
      val - Value.
      Returns:
      Hexadecimal string.
    • toString

      public static String toString(char val)
      Convert the given char value to a four-digit hexadecimal string.
      Parameters:
      val - Value.
      Returns:
      Hexadecimal string.
    • toString

      public static String toString(short val)
      Convert the given short value to a four-digit hexadecimal string.
      Parameters:
      val - Value.
      Returns:
      Hexadecimal string.
    • toString

      public static String toString(int val)
      Convert the given int value to an eight-digit hexadecimal string.
      Parameters:
      val - Value.
      Returns:
      Hexadecimal string.
    • toString

      public static String toString(long val)
      Convert the given long value to a sixteen-digit hexadecimal string.
      Parameters:
      val - Value.
      Returns:
      Hexadecimal string.
    • toString

      public static String toString(byte[] val)
      Convert the given byte array to a hexadecimal string. Each byte is converted to two hexadecimal digits.
      Parameters:
      val - Byte array.
      Returns:
      Hexadecimal string.
      Throws:
      NullPointerException - (unchecked exception) Thrown if val is null.
    • toString

      public static String toString(byte[] val, int off, int len)
      Convert a portion of the given byte array to a hexadecimal string. Each byte is converted to two hexadecimal digits.
      Parameters:
      val - Byte array.
      off - Index of first byte to convert.
      len - Number of bytes to convert.
      Returns:
      Hexadecimal string.
      Throws:
      NullPointerException - (unchecked exception) Thrown if val is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if off < 0, len < 0, or off+len > val.length.
    • toByte

      public static byte toByte(String str)
      Convert the given hexadecimal string to a byte value. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. Any extra high-order digits in the string are omitted.
      Parameters:
      str - Hexadecimal string.
      Returns:
      Value.
      Throws:
      NullPointerException - (unchecked exception) Thrown if str is null.
      IllegalArgumentException - (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
    • toChar

      public static char toChar(String str)
      Convert the given hexadecimal string to a char value. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. Any extra high-order digits in the string are omitted.
      Parameters:
      str - Hexadecimal string.
      Returns:
      Value.
      Throws:
      NullPointerException - (unchecked exception) Thrown if str is null.
      IllegalArgumentException - (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
    • toShort

      public static short toShort(String str)
      Convert the given hexadecimal string to a short value. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. Any extra high-order digits in the string are omitted.
      Parameters:
      str - Hexadecimal string.
      Returns:
      Value.
      Throws:
      NullPointerException - (unchecked exception) Thrown if str is null.
      IllegalArgumentException - (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
    • toInt

      public static int toInt(String str)
      Convert the given hexadecimal string to an int value. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. Any extra high-order digits in the string are omitted.
      Parameters:
      str - Hexadecimal string.
      Returns:
      Value.
      Throws:
      NullPointerException - (unchecked exception) Thrown if str is null.
      IllegalArgumentException - (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
    • toLong

      public static long toLong(String str)
      Convert the given hexadecimal string to a long value. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. Any extra high-order digits in the string are omitted.
      Parameters:
      str - Hexadecimal string.
      Returns:
      Value.
      Throws:
      NullPointerException - (unchecked exception) Thrown if str is null.
      IllegalArgumentException - (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
    • toByteArray

      public static byte[] toByteArray(String str)
      Convert the given hexadecimal string to a sequence of bytes stored in a new byte array. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. The value is stored in a newly allocated byte array, with the least significant byte stored at the highest index and the most significant byte stored at the lowest index. The array's length is sufficient to hold the entire converted string. The newly allocated byte array is returned.
      Parameters:
      str - Hexadecimal string.
      Returns:
      Value (byte array).
      Throws:
      NullPointerException - (unchecked exception) Thrown if str is null.
      IllegalArgumentException - (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
    • toByteArray

      public static void toByteArray(String str, byte[] val)
      Convert the given hexadecimal string to a sequence of bytes stored in the given byte array. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. The value is stored in val from highest index to lowest index, with the least significant byte stored at index val.length-1. If the converted string requires more than val.length bytes, the extra digits at the beginning of the string are not converted. If the converted string requires fewer than val.length bytes, the extra bytes at the beginning of val are set to 0.
      Parameters:
      str - Hexadecimal string.
      val - Byte array in which to store converted value.
      Throws:
      NullPointerException - (unchecked exception) Thrown if str is null or val is null.
      IllegalArgumentException - (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
    • toByteArray

      public static void toByteArray(String str, byte[] val, int off, int len)
      Convert the given hexadecimal string to a sequence of bytes stored in a portion of the given byte array. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. The value is stored in val from highest index to lowest index, with the least significant byte stored at index off+len-1. If the converted string requires more than len bytes, the extra digits at the beginning of the string are not converted. If the converted string requires fewer than len bytes, the extra bytes starting at index off of val are set to 0.
      Parameters:
      str - Hexadecimal string.
      val - Byte array in which to store converted value.
      off - Index of first byte to store.
      len - Number of bytes to store.
      Throws:
      NullPointerException - (unchecked exception) Thrown if str is null or val is null.
      IllegalArgumentException - (unchecked exception) Thrown if off < 0, len < 0, or off+len > val.length. Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').