org.apache.pdfbox.util

## Class Hex

- java.lang.Object
- - org.apache.pdfbox.util.Hex

    public final class Hex
    extends Object

Utility functions for hex encoding.

Author:
  John Hewson

### Method Summary

All Methods Static Methods Concrete Methods
`static byte[]` `decodeBase64(String base64Value)` Decode a base64 String.
`static byte[]` `decodeHex(String s)` Decodes a hex String into a byte array.
`static byte[]` `getBytes(byte b)` Returns the bytes corresponding to the ASCII hex encoding of the given byte.
`static byte[]` `getBytes(byte[] bytes)` Returns the bytes corresponding to the ASCII hex encoding of the given bytes.
`static char[]` `getChars(short num)` Returns the characters corresponding to the ASCII hex encoding of the given short.
`static char[]` `getCharsUTF16BE(String text)` Takes the characters in the given string, convert it to bytes in UTF16-BE format and build a char array that corresponds to the ASCII hex encoding of the resulting bytes.
`static int` `getHexValue(char c)` Converts a given character to its corresponding hexadecimal value.
`static String` `getString(byte b)` Returns a hex string of the given byte.
`static String` `getString(byte[] bytes)` Returns a hex string of the given byte array.
`static void` `writeHexByte(byte b, OutputStream output)` Writes the given byte as hex value to the given output stream.
`static void` `writeHexBytes(byte[] bytes, OutputStream output)` Writes the given byte array as hex value to the given output stream.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Method Detail

#### getString

    public static String getString(byte b)

Returns a hex string of the given byte.

Parameters:
  `b` - the byte to be converted
Returns:
  the hex string representing the given byte

#### getString

    public static String getString(byte[] bytes)

Returns a hex string of the given byte array.

Parameters:
  `bytes` - the bytes to be converted
Returns:
  the hex string representing the given bytes

#### getBytes

    public static byte[] getBytes(byte b)

Returns the bytes corresponding to the ASCII hex encoding of the given byte.

Parameters:
  `b` - the byte to be converted
Returns:
  the ASCII hex encoding of the given byte

#### getBytes

    public static byte[] getBytes(byte[] bytes)

Returns the bytes corresponding to the ASCII hex encoding of the given bytes.

Parameters:
  `bytes` - the bytes to be converted
Returns:
  the ASCII hex encoding of the given bytes

#### getChars

    public static char[] getChars(short num)

Returns the characters corresponding to the ASCII hex encoding of the given short.

Parameters:
  `num` - the short value to be converted
Returns:
  the ASCII hex encoding of the given short value

#### getCharsUTF16BE

    public static char[] getCharsUTF16BE(String text)

Takes the characters in the given string, convert it to bytes in UTF16-BE format and build a char array that corresponds to the ASCII hex encoding of the resulting bytes. Example:

       getCharsUTF16BE("ab") == new char[]{'0','0','6','1','0','0','6','2'}

Parameters:
  `text` - The string to convert
Returns:
  The string converted to hex

#### writeHexByte

    public static void writeHexByte(byte b,
                                    OutputStream output)
                             throws IOException

Writes the given byte as hex value to the given output stream.

Parameters:
  `b` - the byte to be written
  `output` - the output stream to be written to
Throws:
  `IOException` - exception if anything went wrong

#### writeHexBytes

    public static void writeHexBytes(byte[] bytes,
                                     OutputStream output)
                              throws IOException

Writes the given byte array as hex value to the given output stream.

Parameters:
  `bytes` - the byte array to be written
  `output` - the output stream to be written to
Throws:
  `IOException` - exception if anything went wrong

#### decodeBase64

    public static byte[] decodeBase64(String base64Value)

Decode a base64 String.

Parameters:
  `base64Value` - a base64 encoded String.
Returns:
  the decoded String as a byte array.
Throws:
  `IllegalArgumentException` - if this isn't a base64 encoded string.

#### decodeHex

    public static byte[] decodeHex(String s)

Decodes a hex String into a byte array.

Parameters:
  `s` - A String with ASCII hex.
Returns:
  decoded byte array.

#### getHexValue

    public static int getHexValue(char c)

Converts a given character to its corresponding hexadecimal value. Valid characters are '0'-'9', 'A'-'F', or 'a'-'f'. Returns -256 for invalid characters.

The value of -256 is chosen so that to hex digits can be combined before checking for an invalid hex string

Parameters:
  `c` - the character to be converted to a hexadecimal value
Returns:
  the hexadecimal value of the character, or -256 if the character is invalid