public class Hex extends Object implements BinaryEncoder, BinaryDecoder
DEFAULT_CHARSET_NAME
This class is thread-safe.| Modifier and Type | Field and Description |
|---|---|
static Charset |
DEFAULT_CHARSET
Default charset is
StandardCharsets.UTF_8. |
static String |
DEFAULT_CHARSET_NAME
Default charset name is
CharEncoding.UTF_8. |
| Constructor and Description |
|---|
Hex()
Creates a new codec with the default charset name
DEFAULT_CHARSET |
Hex(Charset charset)
Creates a new codec with the given Charset.
|
Hex(String charsetName)
Creates a new codec with the given charset name.
|
| Modifier and Type | Method and Description |
|---|---|
byte[] |
decode(byte[] array)
Converts an array of character bytes representing hexadecimal values into an array of bytes of those same values.
|
byte[] |
decode(ByteBuffer buffer)
Converts a buffer of character bytes representing hexadecimal values into an array of bytes of those same values.
|
Object |
decode(Object object)
Converts a String or an array of character bytes representing hexadecimal values into an array of bytes of those
same values.
|
static byte[] |
decodeHex(char[] data)
Converts an array of characters representing hexadecimal values into an array of bytes of those same values.
|
static int |
decodeHex(char[] data,
byte[] out,
int outOffset)
Converts an array of characters representing hexadecimal values into an array of bytes of those same values.
|
static byte[] |
decodeHex(String data)
Converts a String representing hexadecimal values into an array of bytes of those same values.
|
byte[] |
encode(byte[] array)
Converts an array of bytes into an array of bytes for the characters representing the hexadecimal values of each
byte in order.
|
byte[] |
encode(ByteBuffer array)
Converts byte buffer into an array of bytes for the characters representing the hexadecimal values of each byte
in order.
|
Object |
encode(Object object)
Converts a String or an array of bytes into an array of characters representing the hexadecimal values of each
byte in order.
|
static char[] |
encodeHex(byte[] data)
Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
|
static char[] |
encodeHex(byte[] data,
boolean toLowerCase)
Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
|
protected static char[] |
encodeHex(byte[] data,
char[] toDigits)
Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
|
static char[] |
encodeHex(byte[] data,
int dataOffset,
int dataLen,
boolean toLowerCase)
Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
|
static void |
encodeHex(byte[] data,
int dataOffset,
int dataLen,
boolean toLowerCase,
char[] out,
int outOffset)
Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
|
static char[] |
encodeHex(ByteBuffer data)
Converts a byte buffer into an array of characters representing the hexadecimal values of each byte in order.
|
static char[] |
encodeHex(ByteBuffer data,
boolean toLowerCase)
Converts a byte buffer into an array of characters representing the hexadecimal values of each byte in order.
|
protected static char[] |
encodeHex(ByteBuffer byteBuffer,
char[] toDigits)
Converts a byte buffer into an array of characters representing the hexadecimal values of each byte in order.
|
static String |
encodeHexString(byte[] data)
Converts an array of bytes into a String representing the hexadecimal values of each byte in order.
|
static String |
encodeHexString(byte[] data,
boolean toLowerCase)
Converts an array of bytes into a String representing the hexadecimal values of each byte in order.
|
static String |
encodeHexString(ByteBuffer data)
Converts a byte buffer into a String representing the hexadecimal values of each byte in order.
|
static String |
encodeHexString(ByteBuffer data,
boolean toLowerCase)
Converts a byte buffer into a String representing the hexadecimal values of each byte in order.
|
Charset |
getCharset()
Gets the charset.
|
String |
getCharsetName()
Gets the charset name.
|
protected static int |
toDigit(char ch,
int index)
Converts a hexadecimal character to an integer.
|
String |
toString()
Returns a string representation of the object, which includes the charset name.
|
public static final Charset DEFAULT_CHARSET
StandardCharsets.UTF_8.public static final String DEFAULT_CHARSET_NAME
CharEncoding.UTF_8.public Hex()
DEFAULT_CHARSETpublic Hex(Charset charset)
charset - the charset.public Hex(String charsetName)
charsetName - the charset name.UnsupportedCharsetException - If the named charset is unavailablepublic static byte[] decodeHex(char[] data)
throws DecoderException
data - An array of characters containing hexadecimal digitsDecoderException - Thrown if an odd number of characters or illegal characters are suppliedpublic static int decodeHex(char[] data,
byte[] out,
int outOffset)
throws DecoderException
data - An array of characters containing hexadecimal digitsout - A byte array to contain the binary data decoded from the supplied char array.outOffset - The position within out to start writing the decoded bytes.out.DecoderException - Thrown if an odd number of characters or illegal characters are suppliedpublic static byte[] decodeHex(String data) throws DecoderException
data - A String containing hexadecimal digitsDecoderException - Thrown if an odd number of characters or illegal characters are suppliedpublic static char[] encodeHex(byte[] data)
data - a byte[] to convert to hex characterspublic static char[] encodeHex(byte[] data,
boolean toLowerCase)
data - a byte[] to convert to Hex characterstoLowerCase - true converts to lowercase, false to uppercaseprotected static char[] encodeHex(byte[] data,
char[] toDigits)
data - a byte[] to convert to hex characterstoDigits - the output alphabet (must contain at least 16 chars)public static char[] encodeHex(byte[] data,
int dataOffset,
int dataLen,
boolean toLowerCase)
data - a byte[] to convert to hex charactersdataOffset - the position in data to start encoding fromdataLen - the number of bytes from dataOffset to encodetoLowerCase - true converts to lowercase, false to uppercasepublic static void encodeHex(byte[] data,
int dataOffset,
int dataLen,
boolean toLowerCase,
char[] out,
int outOffset)
data - a byte[] to convert to hex charactersdataOffset - the position in data to start encoding fromdataLen - the number of bytes from dataOffset to encodetoLowerCase - true converts to lowercase, false to uppercaseout - a char[] which will hold the resultant appropriate characters from the alphabet.outOffset - the position within out at which to start writing the encoded characters.public static char[] encodeHex(ByteBuffer data)
All bytes identified by Buffer.remaining() will be used; after this method
the value remaining() will be zero.
data - a byte buffer to convert to hex characterspublic static char[] encodeHex(ByteBuffer data, boolean toLowerCase)
All bytes identified by Buffer.remaining() will be used; after this method
the value remaining() will be zero.
data - a byte buffer to convert to hex characterstoLowerCase - true converts to lowercase, false to uppercaseprotected static char[] encodeHex(ByteBuffer byteBuffer, char[] toDigits)
All bytes identified by Buffer.remaining() will be used; after this method
the value remaining() will be zero.
byteBuffer - a byte buffer to convert to hex characterstoDigits - the output alphabet (must be at least 16 characters)public static String encodeHexString(byte[] data)
data - a byte[] to convert to hex characterspublic static String encodeHexString(byte[] data, boolean toLowerCase)
data - a byte[] to convert to hex characterstoLowerCase - true converts to lowercase, false to uppercasepublic static String encodeHexString(ByteBuffer data)
All bytes identified by Buffer.remaining() will be used; after this method
the value remaining() will be zero.
data - a byte buffer to convert to hex characterspublic static String encodeHexString(ByteBuffer data, boolean toLowerCase)
All bytes identified by Buffer.remaining() will be used; after this method
the value remaining() will be zero.
data - a byte buffer to convert to hex characterstoLowerCase - true converts to lowercase, false to uppercaseprotected static int toDigit(char ch,
int index)
throws DecoderException
ch - A character to convert to an integer digitindex - The index of the character in the sourceDecoderException - Thrown if ch is an illegal hex characterpublic byte[] decode(byte[] array)
throws DecoderException
decode in interface BinaryDecoderarray - An array of character bytes containing hexadecimal digitsDecoderException - Thrown if an odd number of characters is supplied to this functiondecodeHex(char[])public byte[] decode(ByteBuffer buffer) throws DecoderException
All bytes identified by Buffer.remaining() will be used; after this method
the value remaining() will be zero.
buffer - An array of character bytes containing hexadecimal digitsDecoderException - Thrown if an odd number of characters is supplied to this functiondecodeHex(char[])public Object decode(Object object) throws DecoderException
decode in interface Decoderobject - A String, ByteBuffer, byte[], or an array of character bytes containing hexadecimal digitsDecoderException - Thrown if an odd number of characters is supplied to this function or the object is not
a String or char[]decodeHex(char[])public byte[] encode(byte[] array)
The conversion from hexadecimal characters to the returned bytes is performed with the charset named by
getCharset().
encode in interface BinaryEncoderarray - a byte[] to convert to hex charactersencodeHex(byte[])public byte[] encode(ByteBuffer array)
The conversion from hexadecimal characters to the returned bytes is performed with the charset named by
getCharset().
All bytes identified by Buffer.remaining() will be used; after this method
the value remaining() will be zero.
array - a byte buffer to convert to hex charactersencodeHex(byte[])public Object encode(Object object) throws EncoderException
The conversion from hexadecimal characters to bytes to be encoded to performed with the charset named by
getCharset().
encode in interface Encoderobject - a String, ByteBuffer, or byte[] to convert to hex charactersEncoderException - Thrown if the given object is not a String or byte[]encodeHex(byte[])public Charset getCharset()
public String getCharsetName()
Copyright © 2002–2020 The Apache Software Foundation. All rights reserved.