com.buildforge.services.common.text
Class Hex

java.lang.Object
  extended by com.buildforge.services.common.text.Hex

public class Hex
extends java.lang.Object

Fast hex encode/decode


Method Summary
static byte[] bytesToHex(byte[] in)
          Converts a sequence of bytes into a sequence of hex digits representing them.
static byte[] bytesToHex(byte[] in, int offset, int length)
          Converts a sequence of bytes into a sequence of hex digits representing them.
static byte[] bytesToHex(java.nio.ByteBuffer bbuf)
          Converts a sequence of bytes into a sequence of hex digits representing them.
static int decode(int code)
          Reverses the escaping of an arbitrary UTF-16 character value.
static int encode(char c)
          Given a character, returns an integer value that would represent the character as a hex string suitable for use in a "\\u"-style escape sequence.
static byte hexDigit(int b)
          Maps hex values to the ASCII values that represent them.
static byte[] hexToBytes(byte[] in)
          Converts a sequence of hex digit bytes into the sequence of bytes that they represent.
static byte[] hexToBytes(byte[] in, int offset, int length)
          Converts a sequence of hex digit bytes into the sequence of bytes that they represent.
static byte[] hexToBytes(java.nio.ByteBuffer bbuf)
          Converts a sequence of hex digit bytes into the sequence of bytes that they represent.
static java.lang.String hexToString(java.lang.String hex)
          Converts a hexadecimal string of the UTF-8 bytes that encode the original string's characters back to the original string.
static java.lang.String[] hexToString(java.lang.String[] hex)
          Converts an array of hexadecimal strings of the UTF-8 bytes that encode the original strings' characters back to the original array of strings.
static byte hexValue(int b)
          Maps ASCII character values to their corresponding hex values.
static java.lang.String stringToHex(java.lang.String text)
          Converts a string to a hexadecimal string of the UTF-8 bytes that encode the original string's characters.
static java.lang.String[] stringToHex(java.lang.String[] text)
          Converts an array of strings to an array of hexadecimal strings of the UTF-8 bytes that encode the characters of the original array's strings.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

hexDigit

public static byte hexDigit(int b)
Maps hex values to the ASCII values that represent them.

Parameters:
b - the value to map
Returns:
the ASCII character for that hex value (using uppercase)
Throws:
java.lang.IndexOutOfBoundsException - if b is either negative or larger than 0xF.

hexValue

public static byte hexValue(int b)
Maps ASCII character values to their corresponding hex values.

Parameters:
b - the hex character for which to return the numeric value
Returns:
the value of the hex digit, or -1 if b does not correspond to a valid hex digit
Throws:
java.lang.IndexOutOfBoundsException - if b is negative

encode

public static int encode(char c)
Given a character, returns an integer value that would represent the character as a hex string suitable for use in a "\\u"-style escape sequence. For example, if c is 'δΈͺ', then the return value is 0x34453241, which is the 4-byte string "4E2A" expressed as an integer value. This method is intended for use in creating values for use with the AbstractProtocolImpl.write4(int) method, which can be used to write the hex value to the output stream after '\\' and 'u' have been written.

Parameters:
c - the char to be escaped
Returns:
the integer code for use with AbstractProtocolImpl.write4(int)

decode

public static int decode(int code)
Reverses the escaping of an arbitrary UTF-16 character value. This compliments the escaping operation performed by encode(char).

Parameters:
code - the 4-byte sequence of ASCII codes for the hexadecimal representation of the char value that was escaped, expressed as a single integer value (in big endian)
Returns:
the original character code that was escaped, or -1 if the integer does not contain an appropriate value for decoding in this way

stringToHex

public static java.lang.String stringToHex(java.lang.String text)
Converts a string to a hexadecimal string of the UTF-8 bytes that encode the original string's characters.

Parameters:
text - the string to encode as hex
Returns:
the encoded string

stringToHex

public static java.lang.String[] stringToHex(java.lang.String[] text)
Converts an array of strings to an array of hexadecimal strings of the UTF-8 bytes that encode the characters of the original array's strings.

Parameters:
text - the array of strings to encode as hex
Returns:
the array of encoded strings

hexToString

public static java.lang.String hexToString(java.lang.String hex)
Converts a hexadecimal string of the UTF-8 bytes that encode the original string's characters back to the original string.

Parameters:
hex - the string to decode as hex
Returns:
the decoded string

hexToString

public static java.lang.String[] hexToString(java.lang.String[] hex)
Converts an array of hexadecimal strings of the UTF-8 bytes that encode the original strings' characters back to the original array of strings.

Parameters:
hex - the array of strings to decode as hex
Returns:
the array of decoded strings

bytesToHex

public static byte[] bytesToHex(byte[] in)
Converts a sequence of bytes into a sequence of hex digits representing them. A call to this method is exactly equivilant to bytesToHex(in,0,in.length).

Parameters:
in - the byte array to encode as hex digits
Returns:
the encoded byte array

bytesToHex

public static byte[] bytesToHex(java.nio.ByteBuffer bbuf)
Converts a sequence of bytes into a sequence of hex digits representing them. A call to this method is essentially equivilant to bytesToHex(byte[],int,int), but uses a byte buffer instead of a byte array as the source of the bytes to encode as hex.

Parameters:
bbuf - the byte buffer to encode as hex digits
Returns:
the encoded byte array

bytesToHex

public static byte[] bytesToHex(byte[] in,
                                int offset,
                                int length)
Converts a sequence of bytes into a sequence of hex digits representing them. In other words, if all the byte arrays are interpreted as US-ASCII characters, then the values for "Example" become those for "4578616D706C65".

Parameters:
in - the byte array to encode as hex digits
offset - the offset into the array of the bytes to encode
length - the length (in bytes) of the data to encode
Returns:
the encoded byte array

hexToBytes

public static byte[] hexToBytes(byte[] in)
Converts a sequence of hex digit bytes into the sequence of bytes that they represent. A call to this method is exactly equivilant to hexToBytes(in,0,in.length).

Parameters:
in - the byte array to decode as hex digits
Returns:
the decoded bytes

hexToBytes

public static byte[] hexToBytes(java.nio.ByteBuffer bbuf)
Converts a sequence of hex digit bytes into the sequence of bytes that they represent. A call to this method is essentially equivilant to hexToBytes(byte[],int,int), but uses a byte buffer instead of a byte array as the source of the bytes to decode as hex.

Parameters:
bbuf - the byte buffer to decode as hex digits
Returns:
the decoded bytes

hexToBytes

public static byte[] hexToBytes(byte[] in,
                                int offset,
                                int length)
Converts a sequence of hex digit bytes into the sequence of bytes that they represent. In other words, if all the byte arrays are interpreted as US-ASCII characters, then the values for "4578616d706c65" become those for "Example". Both upper- (0x41-0x46) and lowercase (0x61-0x66) letters are accepted. Invalid input combinations produce 0x7F (DEL).

Parameters:
in - the byte array to decode as hex digits
offset - the offset into the array of the bytes to decode
length - the length (in bytes) of the data to decode
Returns:
the decoded bytes