org.jcoderz.commons.util
Class HexUtil

java.lang.Object
  extended by org.jcoderz.commons.util.HexUtil

public final class HexUtil
extends Object

This class converts byte array data from and to hex and also provides a hexdump method.

Author:
Albrecht Messner

Method Summary
static String bytesToHex(byte[] data)
          Converts a byte array into an upper-case hex string, starting at the first byte and converting the whole array.
static String bytesToHex(byte[] data, int offset, int length)
          Converts a byte array into an upper-case hex string, starting at the given offset and converting the given number of bytes.
static String dump(byte[] data)
          Produces a hexdump of the given byte array with a formatting as in "hexdump -C" (canonical hex + ASCII display).
static byte[] stringToBytes(String s)
          Convert the given hex string to a byte array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

bytesToHex

public static String bytesToHex(byte[] data,
                                int offset,
                                int length)
                         throws IndexOutOfBoundsException
Converts a byte array into an upper-case hex string, starting at the given offset and converting the given number of bytes.

Parameters:
data - the byte data to convert to hex
offset - the start offset in the byte array
length - the number of bytes to convert
Returns:
null if data was null, an empty string if data.length == 0, and the hex representation of the byte array otherwise
Throws:
IndexOutOfBoundsException - if offset + length > data.lenght

bytesToHex

public static String bytesToHex(byte[] data)
Converts a byte array into an upper-case hex string, starting at the first byte and converting the whole array.

Parameters:
data - the byte data to convert to hex
Returns:
null if data was null, an empty string if data.length == 0, and the hex representation of the byte array otherwise

stringToBytes

public static byte[] stringToBytes(String s)
                            throws IllegalArgumentException
Convert the given hex string to a byte array.

Parameters:
s - the string to convert
Returns:
null if the string is null, an empty byte array if s.length == 0 and a byte array representing the hex string otherwise
Throws:
IllegalArgumentException - if the string is not a multiple of 2 characters long, or if the string contains an invalid hex char

dump

public static String dump(byte[] data)
Produces a hexdump of the given byte array with a formatting as in "hexdump -C" (canonical hex + ASCII display). This formatting contains an address column, sixteen bytes of hex separated by spaces, with an extra space after eight bytes, and an ascii print-out of the bytes enclosed in pipe symbols. Non-ASCII characters are replaced by dots.

Parameters:
data - the byte data to dump
Returns:
a string containing the hexdump, or null if data was null


Copyright 2007 The jCoderZ Project.