Top Description Fields Constructors Methods
org.apache.pdfbox.util

public final Class Hex

extends Object
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .IOException, .OutputStream, java.util.Base64, org.apache.commons.logging.Log, .LogFactory

Utility functions for hex encoding.
Author
John Hewson

Field Summary

Modifier and TypeField and Description
private static final byte[]
HEX_BYTES

for hex conversion.

private static final char[]
private static final Log

Constructor Summary

AccessConstructor and Description
private
Hex()

Method Summary

Modifier and TypeMethod and Description
public static byte[]

Returns:

the decoded String as a byte array.
decodeBase64
(String
a base64 encoded String.
base64Value
)

Decode a base64 String.

public static byte[]

Returns:

decoded byte array.
decodeHex
(String
A String with ASCII hex.
s
)

Decodes a hex String into a byte array.

public static byte[]

Returns:

the ASCII hex encoding of the given byte
getBytes
(byte
the byte to be converted
b
)

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

public static byte[]

Returns:

the ASCII hex encoding of the given bytes
getBytes
(byte[]
the bytey to be converted
bytes
)

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

public static char[]

Returns:

the ASCII hex encoding of the given short value
getChars
(short
the short value to be converted
num
)

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

public static char[]

Returns:

The string converted to hex
getCharsUTF16BE
(String
The string to convert
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.

private static int

Returns:

the high nibble
getHighNibble
(byte
the given byte
b
)

Get the high nibble of the given byte.

private static int

Returns:

the low nibble
getLowNibble
(byte
the given byte
b
)

Get the low nibble of the given byte.

public static String

Returns:

the hex string representing the given byte
getString
(byte
the byte to be converted
b
)

Returns a hex string of the given byte.

public static String

Returns:

the hex string representing the given bytes
getString
(byte[]
the bytes to be converted
bytes
)

Returns a hex string of the given byte array.

public static void
writeHexByte(byte
the byte to be written
b
,
OutputStream
the output stream to be written to
output
)

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

public static void
writeHexBytes(byte[]
the byte array to be written
bytes
,
OutputStream
the output stream to be written to
output
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

HEX_BYTESback to summary
private static final byte[] HEX_BYTES

for hex conversion. https://stackoverflow.com/questions/2817752/java-code-to-convert-byte-to-hexadecimal

HEX_CHARSback to summary
private static final char[] HEX_CHARS
LOGback to summary
private static final Log LOG

Constructor Detail

Hexback to summary
private Hex()

Method Detail

decodeBase64back to summary
public static byte[] decodeBase64(String base64Value)

Decode a base64 String.

Parameters
base64Value:String

a base64 encoded String.

Returns:byte[]

the decoded String as a byte array.

Exceptions
IllegalArgumentException:
if this isn't a base64 encoded string.
decodeHexback to summary
public static byte[] decodeHex(String s)

Decodes a hex String into a byte array.

Parameters
s:String

A String with ASCII hex.

Returns:byte[]

decoded byte array.

getBytesback to summary
public static byte[] getBytes(byte b)

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

Parameters
b:byte

the byte to be converted

Returns:byte[]

the ASCII hex encoding of the given byte

getBytesback to summary
public static byte[] getBytes(byte[] bytes)

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

Parameters
bytes:byte[]

the bytey to be converted

Returns:byte[]

the ASCII hex encoding of the given bytes

getCharsback to summary
public static char[] getChars(short num)

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

Parameters
num:short

the short value to be converted

Returns:char[]

the ASCII hex encoding of the given short value

getCharsUTF16BEback to summary
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:String

The string to convert

Returns:char[]

The string converted to hex

getHighNibbleback to summary
private static int getHighNibble(byte b)

Get the high nibble of the given byte.

Parameters
b:byte

the given byte

Returns:int

the high nibble

getLowNibbleback to summary
private static int getLowNibble(byte b)

Get the low nibble of the given byte.

Parameters
b:byte

the given byte

Returns:int

the low nibble

getStringback to summary
public static String getString(byte b)

Returns a hex string of the given byte.

Parameters
b:byte

the byte to be converted

Returns:String

the hex string representing the given byte

getStringback to summary
public static String getString(byte[] bytes)

Returns a hex string of the given byte array.

Parameters
bytes:byte[]

the bytes to be converted

Returns:String

the hex string representing the given bytes

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

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

Parameters
b:byte

the byte to be written

output:OutputStream

the output stream to be written to

Exceptions
IOException:
exception if anything went wrong
writeHexBytesback to summary
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:byte[]

the byte array to be written

output:OutputStream

the output stream to be written to

Exceptions
IOException:
exception if anything went wrong