edu.neu.ccs.util
Class Hex

java.lang.Object
  extended byedu.neu.ccs.util.Hex

public class Hex
extends Object

Class Hex provides static methods for the conversion of numeric types to hexadecimal strings and vice versa.

Class Hex cannot be instantiated.

Since:
2.3
Version:
2.3.3

Field Summary
private static long low16bits
           
private static long low32bits
           
private static long low8bits
           
 
Constructor Summary
private Hex()
          Private constructor to prevent instantiation.
 
Method Summary
static String byteToHex(byte b)
          Returns the 2-character hexadecimal String for the given byte.
static String doubleToHex(double d)
          Returns the 16-character hexadecimal String for the given double.
static String floatToHex(float f)
          Returns the 8-character hexadecimal String for the given float.
static byte hexToByte(String s)
          If the given String s is a hexadecimal String and contains at most 2 digits, then the String is converted to a byte and returned.
static double hexToDouble(String s)
          If the given String s is a hexadecimal String and contains exactly 16 digits, then the String is converted to a double and returned.
static float hexToFloat(String s)
          If the given String s is a hexadecimal String and contains exactly 8 digits, then the String is converted to a float and returned.
static int hexToInt(String s)
          If the given String s is a hexadecimal String and contains at most 8 digits, then the String is converted to an int and returned.
static long hexToLong(String s)
          If the given String s is a hexadecimal String and contains at most 16 digits, then the String is converted to a long and returned.
static long hexToLong(String s, int maxdigits)
          If the given String s is a hexadecimal String and contains at most maxdigits, then the String is converted to a long and returned.
static short hexToShort(String s)
          If the given String s is a hexadecimal String and contains at most 4 digits, then the String is converted to a short and returned.
static String intToHex(int i)
          Returns the 8-character hexadecimal String for the given int.
static boolean isHexDigit(char c)
          Returns whether or not the given char is a hexadecimal digit (0 to 9, A to F, a to f).
static boolean isHexString(String s)
          Returns whether or not the given String contains only hexadecimal digits (0 to 9, A to F, a to f).
static String longToHex(long a)
          Returns the 16-character hexadecimal String for the given long.
static String longToHex(long a, int maxdigits)
          Returns a hexadecimal string for the given long that contains the maxdigits trailing hexadecimal digits.
static StringableFilter maskFilter(long mask)
          Returns a Stringable filter that filters XNumber objects to be those that have zero bits where the given mask has one bits.
static String shortToHex(short s)
          Returns the 4-character hexadecimal String for the given short.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

low8bits

private static final long low8bits
See Also:
Constant Field Values

low16bits

private static final long low16bits
See Also:
Constant Field Values

low32bits

private static final long low32bits
See Also:
Constant Field Values
Constructor Detail

Hex

private Hex()
Private constructor to prevent instantiation.

Method Detail

byteToHex

public static String byteToHex(byte b)
Returns the 2-character hexadecimal String for the given byte.

Parameters:
b - the byte to convert
Returns:
its 2-character hexadecimal String

shortToHex

public static String shortToHex(short s)
Returns the 4-character hexadecimal String for the given short.

Parameters:
s - the short to convert
Returns:
its 4-character hexadecimal String

intToHex

public static String intToHex(int i)
Returns the 8-character hexadecimal String for the given int.

Parameters:
i - the int to convert
Returns:
its 8-character hexadecimal String

longToHex

public static String longToHex(long a)
Returns the 16-character hexadecimal String for the given long.

Parameters:
a - the long to convert
Returns:
its 16-character hexadecimal String

floatToHex

public static String floatToHex(float f)

Returns the 8-character hexadecimal String for the given float.

Uses Float.floatToRawIntBits internally.

Parameters:
f - the float to convert
Returns:
its 8-character hexadecimal String

doubleToHex

public static String doubleToHex(double d)

Returns the 16-character hexadecimal String for the given double.

Uses Double.doubleToRawLongBits internally.

Parameters:
d - the double to convert
Returns:
its 16-character hexadecimal String

longToHex

public static String longToHex(long a,
                               int maxdigits)

Returns a hexadecimal string for the given long that contains the maxdigits trailing hexadecimal digits.

This method is a helper method for the other methods in this class.

Parameters:
a - the long to convert
maxdigits - the number of trailing digits to provide
Returns:
the trailing digit hexadecimal String with maxdigits digits

hexToByte

public static byte hexToByte(String s)
                      throws ParseException

If the given String s is a hexadecimal String and contains at most 2 digits, then the String is converted to a byte and returned.

Throws a ParseException if the String is not a hexadecimal String or its length is greater than 2.

Parameters:
s - the hexadecimal String
Returns:
the byte value of s
Throws:
ParseException - if s is not hexadecimal with at most 2 digits

hexToShort

public static short hexToShort(String s)
                        throws ParseException

If the given String s is a hexadecimal String and contains at most 4 digits, then the String is converted to a short and returned.

Throws a ParseException if the String is not a hexadecimal String or its length is greater than 4.

Parameters:
s - the hexadecimal String
Returns:
the short value of s
Throws:
ParseException - if s is not hexadecimal with at most 4 digits

hexToInt

public static int hexToInt(String s)
                    throws ParseException

If the given String s is a hexadecimal String and contains at most 8 digits, then the String is converted to an int and returned.

Throws a ParseException if the String is not a hexadecimal String or its length is greater than 8.

Parameters:
s - the hexadecimal String
Returns:
the int value of s
Throws:
ParseException - if s is not hexadecimal with at most 8 digits

hexToLong

public static long hexToLong(String s)
                      throws ParseException

If the given String s is a hexadecimal String and contains at most 16 digits, then the String is converted to a long and returned.

Throws a ParseException if the String is not a hexadecimal String or its length is greater than 16.

Parameters:
s - the hexadecimal String
Returns:
the long value of s
Throws:
ParseException - if s is not hexadecimal with at most 16 digits

hexToFloat

public static float hexToFloat(String s)
                        throws ParseException

If the given String s is a hexadecimal String and contains exactly 8 digits, then the String is converted to a float and returned.

Uses Float.intBitsToFloat internally.

Throws a ParseException if the String is not a hexadecimal String or its length is not exactly 8.

Parameters:
s - the hexadecimal String
Returns:
the float value of s
Throws:
ParseException - if s is not hexadecimal with exactly 8 digits
See Also:
hexToInt(String), Float.intBitsToFloat(int)

hexToDouble

public static double hexToDouble(String s)
                          throws ParseException

If the given String s is a hexadecimal String and contains exactly 16 digits, then the String is converted to a double and returned.

Uses Double.longBitsToDouble internally.

Throws a ParseException if the String is not a hexadecimal String or its length is not exactly 16.

Parameters:
s - the hexadecimal String
Returns:
the double value of s
Throws:
ParseException - if s is not hexadecimal with exactly 16 digits
See Also:
hexToLong(String), Double.longBitsToDouble(long)

hexToLong

public static long hexToLong(String s,
                             int maxdigits)
                      throws ParseException

If the given String s is a hexadecimal String and contains at most maxdigits, then the String is converted to a long and returned.

Throws a ParseException if the given String is null or is not a hexadecimal String, or if its length is greater than maxdigits, or if maxdigits is greater than 16.

This method is a helper method for the other methods in this class.

Parameters:
s - the hexadecimal String
maxdigits - the limit on the length of s
Returns:
the long value of s
Throws:
ParseException

isHexString

public static boolean isHexString(String s)

Returns whether or not the given String contains only hexadecimal digits (0 to 9, A to F, a to f).

Returns false if the given String is null.

Parameters:
s - the String to test
Returns:
whether the given String contains only hexadecimal digits

isHexDigit

public static boolean isHexDigit(char c)
Returns whether or not the given char is a hexadecimal digit (0 to 9, A to F, a to f).

Parameters:
c - the char to test
Returns:
whether the given char is a hexadecimal digit

maskFilter

public static StringableFilter maskFilter(long mask)

Returns a Stringable filter that filters XNumber objects to be those that have zero bits where the given mask has one bits.

If the object is of integral type then it is tested as a long but with the high order bits set to zero if necessary prior to the testing.

If the object is of floating type then it is converted to raw bits with the high order bits set to zero if necessary prior to the testing.

Parameters:
mask - the mask that tests for zero bits in the data