de.bielefeld.umweltamt.aui.utils
Class HexString

java.lang.Object
  extended by de.bielefeld.umweltamt.aui.utils.HexString

public class HexString
extends java.lang.Object

Contains utility functions for converting between hexadecimal strings and their equivalent byte buffers or String objects.


Constructor Summary
HexString()
           
 
Method Summary
static java.lang.String bufferToHex(byte[] buffer)
          Returns a string containing the hexadecimal representation of the input byte array.
static java.lang.String bufferToHex(byte[] buffer, int startOffset, int length)
          Returns a string containing the hexadecimal representation of the input byte array.
static byte[] hexToBuffer(java.lang.String hexString)
          Returns a byte array built from the byte values represented by the input hexadecimal string.
static java.lang.String hexToString(java.lang.String hexString)
          Returns a string built from the byte values represented by the input hexadecimal string.
static java.lang.String stringToHex(java.lang.String s)
          Returns a string containing the hexadecimal representation of the input string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HexString

public HexString()
Method Detail

stringToHex

public static java.lang.String stringToHex(java.lang.String s)
Returns a string containing the hexadecimal representation of the input string. Each byte in the input string is converted to a two-digit hexadecimal value. Thus the returned string is twice the length of the input string. The output hex characters are upper case. The conversion assumes the default charset; making the charset explicit could be accomplished by just adding a parameter here and passing it through to getBytes().

Parameters:
s - a string to convert to hex
Returns:
the hex string version of the input string

bufferToHex

public static java.lang.String bufferToHex(byte[] buffer)
Returns a string containing the hexadecimal representation of the input byte array. Each byte in the input array is converted to a two-digit hexadecimal value. Thus the returned string is twice the length of the input byte array. The output hex characters are upper case.

Parameters:
buffer - a buffer to convert to hex
Returns:
the hex string version of the input buffer

bufferToHex

public static java.lang.String bufferToHex(byte[] buffer,
                                           int startOffset,
                                           int length)
Returns a string containing the hexadecimal representation of the input byte array. Each byte in the input array is converted to a two-digit hexadecimal value. Thus the returned string is twice the length of the specified amount of the input byte array. The output hex characters are upper case.

Parameters:
buffer - a buffer to convert to hex
startOffset - the offset of the first byte in the buffer to process
length - the number of bytes in the buffer to process
Returns:
the hex string version of the input buffer

hexToString

public static java.lang.String hexToString(java.lang.String hexString)
                                    throws java.lang.NumberFormatException
Returns a string built from the byte values represented by the input hexadecimal string. That is, each pair of hexadecimal characters in the input string is decoded into the byte value that they represent, and that byte value is appended to a string which is ultimately returned. This function doesn't care whether the hexadecimal characters are upper or lower case, and it also can handle odd-length hex strings by assuming a leading zero digit. If any character in the input string is not a valid hexadecimal digit, it throws a NumberFormatException, in keeping with the behavior of Java functions like Integer.parseInt(). The conversion assumes the default charset; making the charset explicit could be accomplished by just adding a parameter here and passing it through to the String constructor.

Parameters:
hexString - a string of hexadecimal characters
Returns:
a String built from the bytes indicated by the input string
Throws:
java.lang.NumberFormatException

hexToBuffer

public static byte[] hexToBuffer(java.lang.String hexString)
                          throws java.lang.NumberFormatException
Returns a byte array built from the byte values represented by the input hexadecimal string. That is, each pair of hexadecimal characters in the input string is decoded into the byte value that they represent, and that byte value is appended to a byte array which is ultimately returned. This function doesn't care whether the hexadecimal characters are upper or lower case, and it also can handle odd-length hex strings by assuming a leading zero digit. If any character in the input string is not a valid hexadecimal digit, it throws a NumberFormatException, in keeping with the behavior of Java functions like Integer.parseInt().

Parameters:
hexString - a string of hexadecimal characters
Returns:
a byte array built from the bytes indicated by the input string
Throws:
java.lang.NumberFormatException