org.xins.common.text
Class HexConverter

java.lang.Object
  extended byorg.xins.common.text.HexConverter

public class HexConverter
extends Object

Utility class for converting numbers to unsigned hex strings and vice versa.

Since:
XINS 1.0.0
Version:
$Revision: 1.25 $ $Date: 2006/08/28 09:12:31 $
Author:
Ernst de Haan

Method Summary
static boolean isHexDigit(char c)
          Checks if the specified character is a hexadecimal digit.
static byte[] parseHexBytes(String s, int index, int length)
          Parses the specified string as a set of hex digits and converts it to a byte array.
static int parseHexInt(String s)
          Parses the specified 8-digit unsigned hex string.
static int parseHexInt(String s, int index)
          Parses the 8-digit unsigned hex number in the specified string.
static long parseHexLong(String s)
          Parses the specified 16-digit unsigned hex string.
static long parseHexLong(String s, int index)
          Parses the 16-digit unsigned hex number in the specified string.
static String toHexString(byte n)
          Converts the specified byte to an unsigned number hex string.
static String toHexString(byte[] input)
          Converts the specified byte array to an unsigned number hex string.
static String toHexString(char n)
          Converts the specified char to an unsigned number hex string.
static void toHexString(FastStringBuffer buffer, byte n)
          Converts the specified byte to unsigned number and appends it to the specified string buffer.
static void toHexString(FastStringBuffer buffer, char n)
          Converts the specified char to unsigned number and appends it to the specified string buffer.
static void toHexString(FastStringBuffer buffer, int n)
          Converts the specified int to unsigned number and appends it to the specified string buffer.
static void toHexString(FastStringBuffer buffer, long n)
          Converts the specified long to unsigned number and appends it to the specified string buffer.
static void toHexString(FastStringBuffer buffer, short n)
          Converts the specified short to unsigned number and appends it to the specified string buffer.
static String toHexString(int n)
          Converts the specified int to an unsigned number hex string.
static String toHexString(long n)
          Convert the specified long to an unsigned number hex string.
static String toHexString(short n)
          Converts the specified short to an unsigned number hex string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isHexDigit

public static final boolean isHexDigit(char c)
Checks if the specified character is a hexadecimal digit. The following ranges of characters are considered hexadecimal digits:

Parameters:
c - the character to check.
Returns:
true if the specified character is a hexadecimal digit, false otherwise.

toHexString

public static String toHexString(byte[] input)
                          throws IllegalArgumentException
Converts the specified byte array to an unsigned number hex string. The number of characters in the returned string will always be equal to the number of input bytes times 2.

Parameters:
input - the byte[] array to be converted to a hex string.
Returns:
the hex string, cannot be null, the length is always 2 times the length of the input array (i.e. return.length() == (input.length * 2)).
Throws:
IllegalArgumentException - if n == null || n.length < 1.

toHexString

public static String toHexString(byte n)
Converts the specified byte to an unsigned number hex string. The returned string will always consist of 2 hex characters, a zero will be prepended if necessary.

Parameters:
n - the number to be converted to a hex string.
Returns:
the hex string, cannot be null, the length is always 2 (i.e. return.length() == 2).

toHexString

public static String toHexString(short n)
Converts the specified short to an unsigned number hex string. The returned string will always consist of 4 hex characters, zeroes will be prepended as necessary.

Parameters:
n - the number to be converted to a hex string.
Returns:
the hex string, cannot be null, the length is always 4 (i.e. return.length() == 4).

toHexString

public static String toHexString(char n)
Converts the specified char to an unsigned number hex string. The returned string will always consist of 4 hex characters, zeroes will be prepended as necessary.

Parameters:
n - the character to be converted to a hex string.
Returns:
the hex string, cannot be null, the length is always 4 (i.e. return.length() == 4).

toHexString

public static String toHexString(int n)
Converts the specified int to an unsigned number hex string. The returned string will always consist of 8 hex characters, zeroes will be prepended as necessary.

Parameters:
n - the number to be converted to a hex string.
Returns:
the hex string, cannot be null, the length is always 8 (i.e. return.length() == 8).

toHexString

public static String toHexString(long n)
Convert the specified long to an unsigned number hex string. The returned string will always consist of 16 hex characters, zeroes will be prepended as necessary.

Parameters:
n - the number to be converted to a hex string.
Returns:
the hex string, cannot be null, the length is always 16 (i.e. return.length() == 16).

toHexString

public static void toHexString(FastStringBuffer buffer,
                               byte n)
                        throws IllegalArgumentException
Converts the specified byte to unsigned number and appends it to the specified string buffer. Exactly 2 characters will be appended, both between '0' to '9' or between 'a' and 'f'.

Parameters:
buffer - the string buffer to append to, cannot be null.
n - the number to be converted to a hex string.
Throws:
IllegalArgumentException - if buffer == null.
Since:
XINS 1.3.0

toHexString

public static void toHexString(FastStringBuffer buffer,
                               short n)
                        throws IllegalArgumentException
Converts the specified short to unsigned number and appends it to the specified string buffer. Exactly 4 characters will be appended, all between '0' to '9' or between 'a' and 'f'.

Parameters:
buffer - the string buffer to append to, cannot be null.
n - the number to be converted to a hex string.
Throws:
IllegalArgumentException - if buffer == null.
Since:
XINS 1.3.0

toHexString

public static void toHexString(FastStringBuffer buffer,
                               char n)
                        throws IllegalArgumentException
Converts the specified char to unsigned number and appends it to the specified string buffer. Exactly 4 characters will be appended, all between '0' to '9' or between 'a' and 'f'.

Parameters:
buffer - the string buffer to append to, cannot be null.
n - the number to be converted to a hex string.
Throws:
IllegalArgumentException - if buffer == null.
Since:
XINS 1.3.0

toHexString

public static void toHexString(FastStringBuffer buffer,
                               int n)
                        throws IllegalArgumentException
Converts the specified int to unsigned number and appends it to the specified string buffer. Exactly 8 characters will be appended, all between '0' to '9' or between 'a' and 'f'.

Parameters:
buffer - the string buffer to append to, cannot be null.
n - the number to be converted to a hex string.
Throws:
IllegalArgumentException - if buffer == null.

toHexString

public static void toHexString(FastStringBuffer buffer,
                               long n)
                        throws IllegalArgumentException
Converts the specified long to unsigned number and appends it to the specified string buffer. Exactly 16 characters will be appended, all between '0' to '9' or between 'a' and 'f'.

Parameters:
buffer - the string buffer to append to, cannot be null.
n - the number to be converted to a hex string.
Throws:
IllegalArgumentException - if buffer == null.

parseHexBytes

public static byte[] parseHexBytes(String s,
                                   int index,
                                   int length)
                            throws IllegalArgumentException,
                                   NumberFormatException
Parses the specified string as a set of hex digits and converts it to a byte array.

Parameters:
s - the hexadecimal string, cannot be null.
index - the starting index in the string, must be >= 0.
length - the number of characters to convert in the string, must be >= 0.
Returns:
the value of the parsed unsigned hexadecimal string, as an array of bytes.
Throws:
IllegalArgumentException - if s == null || index < 0 || length < 1 || s.length() < index + length).
NumberFormatException - if any of the characters in the specified range of the string is not a hex digit ('0' to '9', 'a' to 'f' and 'A' to 'F').

parseHexInt

public static int parseHexInt(String s,
                              int index)
                       throws IllegalArgumentException,
                              NumberFormatException
Parses the 8-digit unsigned hex number in the specified string.

Parameters:
s - the hexadecimal string, cannot be null.
index - the starting index in the string, must be >= 0.
Returns:
the value of the parsed unsigned hexadecimal string.
Throws:
IllegalArgumentException - if s == null || index < 0 || s.length() < index + 8).
NumberFormatException - if any of the characters in the specified range of the string is not a hex digit ('0' to '9' and 'a' to 'f').

parseHexInt

public static int parseHexInt(String s)
                       throws IllegalArgumentException,
                              NumberFormatException
Parses the specified 8-digit unsigned hex string.

Parameters:
s - the hexadecimal string, cannot be null and must have size 8 (i.e. s.length() == 8).
Returns:
the value of the parsed unsigned hexadecimal string.
Throws:
IllegalArgumentException - if s == null || s.length() != 8.
NumberFormatException - if any of the characters in the specified string is not a hex digit ('0' to '9' and 'a' to 'f').

parseHexLong

public static long parseHexLong(String s,
                                int index)
                         throws IllegalArgumentException,
                                NumberFormatException
Parses the 16-digit unsigned hex number in the specified string.

Parameters:
s - the hexadecimal string, cannot be null.
index - the starting index in the string, must be >= 0.
Returns:
the value of the parsed unsigned hexadecimal string.
Throws:
IllegalArgumentException - if s == null || index < 0 || s.length() < index + 16).
NumberFormatException - if any of the characters in the specified range of the string is not a hex digit ('0' to '9' and 'a' to 'f').

parseHexLong

public static long parseHexLong(String s)
                         throws IllegalArgumentException,
                                NumberFormatException
Parses the specified 16-digit unsigned hex string.

Parameters:
s - the hexadecimal string, cannot be null and must have size 16 (i.e. s.length() == 16).
Returns:
the value of the parsed unsigned hexadecimal string.
Throws:
IllegalArgumentException - if s == null || s.length() != 16.
NumberFormatException - if any of the characters in the specified string is not a hex digit ('0' to '9' and 'a' to 'f').


See http://www.xins.org/.