tribble.util
Class Hex

java.lang.Object
  extended by tribble.util.Hex

public abstract class Hex
extends java.lang.Object

Hexadecimal encoding and decoding methods.

Hexadecimal encoding, a.k.a. radix-16 encoding, is a form of encoding binary data as hexadecimal digits. (Each hexadecimal digit is 4 bits, providing 16 distinct binary values, hence the term hexadecimal or radix-16.) Two hexadecimal digits can be encoded in a single 8-bit byte.

Converting straight binary data (as a sequence of 8-bit bytes) into a hexadecimal (radix-16) representation is the process of converting each byte of the data into two hexadecimal digit characters.

Converting hexadecimal (radix-16) encoded data (as a sequence of 8-bit characters) back into straight binary data is the reverse operation, substituting each pair of hexadecimal digit characters with its corresponding 8-bit byte value.

For example, the 8-byte array:

    { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF } 
is represented by the 16-character hexadecimal digit string:
    "0123456789ABCDEF" 

Since:
2003-03-14
Version:
$Revision: 1.1 $ $Date: 2003/03/14 16:59:11 $
Author:
David R. Tribble, david@tribble.com.
Copyright 2003 by David R. Tribble, all rights reserved.
Permission is granted to freely use and distribute this source code provided that the original copyright and authorship notices remain intact.
See Also:
HexDump

Field Summary
static int SERIES
          Class revision number.
 
Method Summary
static byte[] decode(java.lang.String chars)
          Decode hexadecimal characters into binary data.
static int decode(java.lang.String chars, byte[] dec, int off, int len)
          Decode hexadecimal characters into binary data.
static java.lang.String encode(byte[] data)
          Encode binary data as hexadecimal characters.
static java.lang.String encode(byte[] data, int off, int len)
          Encode binary data as hexadecimal characters.
static int fromHex(char ch)
          Convert a hexadecimal (radix-16) digit character into its corresponding 4-bit binary value.
static char toHex(int v)
          Convert a 4-bit binary value into its corresponding hexadecimal (radix-16) printable ASCII digit character.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SERIES

public static final int SERIES
Class revision number.

See Also:
Constant Field Values
Method Detail

toHex

public static final char toHex(int v)
Convert a 4-bit binary value into its corresponding hexadecimal (radix-16) printable ASCII digit character.

Parameters:
v - A 4-bit binary value in the range [0,15].
Returns:
A hexadecimal digit, as a printable ASCII character code; or '?', indicating an invalid value.
Since:
1.1, 2003-03-14

fromHex

public static final int fromHex(char ch)
Convert a hexadecimal (radix-16) digit character into its corresponding 4-bit binary value.

Parameters:
ch - A hexadecimal (radix-16) ASCII digit character.
Returns:
A 4-bit binary value in the range [0,15]; or the value -1, indicating an invalid hexadecimal digit character.
Since:
1.1, 2003-03-14

encode

public static java.lang.String encode(byte[] data)
Encode binary data as hexadecimal characters.

Parameters:
data - Binary data, as an array of bytes (8-bit octets).
Returns:
A printable ASCII string containing the contents of data encoded as hexadecimal (radix-16) digit characters. This string will contain exactly data.length*2 characters.
Since:
1.1, 2003-03-14

encode

public static java.lang.String encode(byte[] data,
                                      int off,
                                      int len)
Encode binary data as hexadecimal characters.

Parameters:
data - Binary data, as an array of bytes (8-bit octets).
off - Index of the first byte (octet) within data to encode.
len - Number of bytes (octets) within data to encode.
Returns:
A printable ASCII string containing the contents of data encoded as hexadecimal (radix-16) digit characters. This string will contain exactly len*2 characters.
Since:
1.1, 2003-03-14

decode

public static byte[] decode(java.lang.String chars)
                     throws java.text.ParseException
Decode hexadecimal characters into binary data.

Parameters:
chars - Printable ASCII string containing binary data encoded as radix-16 characters. Whitespace characters (spaces, tabs, newlines) and control characters are ignored.
Returns:
Binary data, as an array of bytes (8-bit octets). This array will contain no more than chars.length()*2 bytes.
Throws:
java.text.ParseException
Since:
1.1, 2003-03-14

decode

public static int decode(java.lang.String chars,
                         byte[] dec,
                         int off,
                         int len)
                  throws java.text.ParseException
Decode hexadecimal characters into binary data.

Parameters:
chars - Printable ASCII string containing binary data encoded as radix-16 characters. Whitespace characters (spaces, tabs, newlines) and control characters are ignored.
dec - Decoded byte (8-bit octet) array, which is filled with the decoded data.
off - Index of the first byte (8-bit octet) within dec to write to.
len - Number of bytes (8-bit octets) to write into dec.
Returns:
The number of bytes (8-bit octets) actually written into dec. This will be no more than len or chars.length()*2, whichever is less.
Throws:
java.text.ParseException - Thrown if chars is malformed or contains an invalid character code.
Since:
1.1, 2003-03-14