edu.harvard.deas.hyperenc.util
Class HexCoder

java.lang.Object
  extended by edu.harvard.deas.hyperenc.util.HexCoder

public class HexCoder
extends Object

Converts byte sequences into hex strings.


Constructor Summary
HexCoder()
           
 
Method Summary
static byte[] decode(String data)
          Decodes the hex string data and returns the corresponding bytes.
static String encode(byte[] data)
          Encodes data as a hex string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HexCoder

public HexCoder()
Method Detail

encode

public static String encode(byte[] data)
Encodes data as a hex string. data[0] comes first in the string, followed by the others in order. Each byte is encoded as specified by Integer.toHexString(int), except padded with a leading zero as necessary so that each byte is represented by two characters. The length of the string is thus exactly 2*data.length.

Parameters:
data - An array of bytes. May not be null.
Returns:
The hex encoding of data.

decode

public static byte[] decode(String data)
Decodes the hex string data and returns the corresponding bytes. The returned array has size data.length()/2; data must have even length, and only contain characters 0-9 and a-f.

Parameters:
data - A hex-encoded string.
Returns:
The byte array encoded by data.
Throws:
IllegalArgumentException - if data does not have even length, or if data contains an invalid character (one other than 0-9 or a-f)