public class Hex extends Object
Hexadecimal output produced by this class will always use exactly
two characters per byte, in other words hex values lower than or
equal to 'F' will be left-padded with a '0'.
A delimiter can optionally be used to separate the byte values
in the hexadecimal representation. The characters in the delimiter
must be printable characters from the US_ASCII character set
and it must furthermore not contain any of the characters used to
represent the hex values themselves (characters 0-9, A-f and
a-f).
JDK 17 introduces HexFormat class which can often be used instead
of this class.
| Modifier and Type | Class and Description |
|---|---|
static class |
Hex.HexCase
Case used for hex string.
|
| Modifier and Type | Field and Description |
|---|---|
static char[] |
HEX_CHARS_LOWER
Characters allowed in hex representation (lower-case)
|
static char[] |
HEX_CHARS_UPPER
Characters allowed in hex representation (upper-case)
|
static String |
HEX_REGEXP_CHARCLASS
Regular expression character set with the characters that are
allowed in hex representation.
|
| Modifier and Type | Method and Description |
|---|---|
static char[] |
bytesToHex(byte[] bytes,
Hex.HexCase caseType,
String delim)
Converts a byte array to its hexadecimal representation.
|
static String |
bytesToHexStr(byte[] bytes,
Hex.HexCase caseType)
Converts a byte array to its hexadecimal representation and returns
the result as a string.
|
static String |
bytesToHexStr(byte[] bytes,
Hex.HexCase caseType,
String delim)
Converts a byte array to its hexadecimal representation and returns
the result as a string.
|
static StringBuilder |
bytesToHexStr(byte[] bytes,
Hex.HexCase caseType,
StringBuilder sb)
Converts a byte array to its hexadecimal representation and appends
the result to the provided
StringBuilder. |
static StringBuilder |
bytesToHexStr(byte[] bytes,
Hex.HexCase caseType,
String delim,
StringBuilder sb)
Converts a byte array to its hexadecimal representation and appends
the result to the provided
StringBuilder. |
static void |
bytesToHexStreaming(InputStream is,
OutputStream os,
Hex.HexCase caseType,
String delim)
Converts a stream of bytes (from an InputStream) to its hexadecimal
character representation and writes the result to the provided
OutputStream.
|
static void |
bytesToHexStreaming(Path in,
Path out,
Hex.HexCase caseType,
String delim,
int maxCharsPerLine,
OpenOption... outOptions)
Converts the content of a file to its hexadecimal character
representation and writes the result to another file.
|
static char[] |
byteToHex(byte b,
Hex.HexCase caseType)
Converts a single byte to its hexadecimal representation.
|
static byte |
hexCharToByte(char c1,
char c2)
Converts a hex character pair into a byte.
|
static byte[] |
hexStrToBytes(char[] chars)
Converts a character sequence (a string) consisting of hexadecimal
characters into a byte array.
|
static byte[] |
hexStrToBytes(char[] chars,
String delim)
Converts a character sequence (a string) consisting of hexadecimal
characters into a byte array.
|
static byte[] |
hexStrToBytes(CharSequence s)
Converts a character sequence (a string) consisting of hexadecimal
characters into a byte array.
|
static byte[] |
hexStrToBytes(CharSequence s,
String delim)
Converts a character sequence (a string) consisting of hexadecimal
characters into a byte array.
|
static void |
hexToBytesStreaming(InputStream is,
OutputStream os,
String delim)
Converts an input stream of hexadecimal characters characters into bytes
and writes the resulting bytes to output stream.
|
static void |
hexToBytesStreaming(Path in,
Path out,
String delim,
OpenOption... outOptions)
Converts the content of a hex file file to its byte representation and
writes the result to another file.
|
static boolean |
isHexChar(char c)
Tests if a character is a hex character.
|
static boolean |
isHexStr(String str)
Tests if a string only contains hex characters.
|
public static final char[] HEX_CHARS_LOWER
public static final char[] HEX_CHARS_UPPER
public static final String HEX_REGEXP_CHARCLASS
public static char[] byteToHex(byte b,
Hex.HexCase caseType)
b - inputcaseType - if the hexadecimal representation should be in upper or lower casepublic static char[] bytesToHex(byte[] bytes,
Hex.HexCase caseType,
String delim)
bytes - inputcaseType - if the hexadecimal representation should be in upper or lower casedelim - optional delimiter between byte values in the encoded output, may be nullpublic static String bytesToHexStr(byte[] bytes, Hex.HexCase caseType, String delim)
bytes - inputcaseType - if the hexadecimal representation should be in upper or lower casedelim - optional delimiter between byte values in the encoded output, may be nullpublic static String bytesToHexStr(byte[] bytes, Hex.HexCase caseType)
bytes - inputcaseType - if the hexadecimal representation should be in upper or lower casepublic static StringBuilder bytesToHexStr(byte[] bytes, Hex.HexCase caseType, String delim, StringBuilder sb)
StringBuilder.bytes - inputcaseType - if the hexadecimal representation should be in upper or lower casedelim - optional delimiter between byte values in the encoded output, may be nullsb - the StringBuilder to append to (not null)public static StringBuilder bytesToHexStr(byte[] bytes, Hex.HexCase caseType, StringBuilder sb)
StringBuilder.bytes - inputcaseType - if the hexadecimal representation should be in upper or lower casesb - the StringBuilder to append to (not null)public static void bytesToHexStreaming(InputStream is, OutputStream os, Hex.HexCase caseType, String delim) throws IOException
The method reads from the InputStream until end-of-stream or an error occurs. Neither the InputStream nor the OutputStream is closed by the method.
Note that representing large binary data structures in base16 encoded
format (another word for hexadecimal character representation) is very
inefficient compared to for example base64 encoding. Base64
encoding is almost always preferable.
When dealing with files it is likely to be considerably faster if a buffered stream is used.
is - inputos - outputcaseType - if the hexadecimal representation should be in upper or lower casedelim - optional delimiter between byte values in the encoded output, may be nullIOException - if reading from the provided InputStream or writing
to the provided OutputStream causes an exception.bytesToHexStreaming(Path, Path, HexCase, String, int, OpenOption...)public static void bytesToHexStreaming(Path in, Path out, Hex.HexCase caseType, String delim, int maxCharsPerLine, OpenOption... outOptions) throws IOException
Assuming the delimiter is either null or some simple character(s)
like '-' then it can be guaranteed that the output file
will consist only of printable characters: 0-9, A-F
or a-f as well as what is in the optional delimiter.
Using the maxCharsPerLine parameter the output file can optionally be
written with line breaks so that the file will look more like a text
file. The line break character(s) used will be according to platform.
Note that representing large binary data structures in base16 encoded
format (another word for hexadecimal character representation) is very
inefficient compared to for example base64 encoding. Base64
encoding is almost always preferable.
in - input fileout - output filecaseType - if the hexadecimal representation should be in upper or lower casedelim - optional delimiter between byte values in the encoded output, may be nullmaxCharsPerLine - maximum number of characters per line in the output file
or -1 to not break the output into lines.outOptions - options specifying how the output file is opened.IOException - if reading from the input file or writing
to the output file causes an exception.public static byte hexCharToByte(char c1,
char c2)
'7'
and 'D' returns the byte value 125.
The input values must be only the characters 0-9, A-F and
a-f.
c1 - first character of pairc2 - second character of pairIllegalArgumentException - if either c1 or c2 is an illegal character.public static byte[] hexStrToBytes(char[] chars,
String delim)
The input must consist only of characters
0-9, A-F and a-f as well as the characters
from the optional delimiter.
chars - inputdelim - optional delimiter between byte values in the hex string input,
may be null.
If the hex string was created with a delimiter the then same
delimiter must be used here.null if chars is null.HexConversionException - if input character array contains illegal charspublic static byte[] hexStrToBytes(char[] chars)
The input must consist only of characters 0-9, A-F and
a-f.
chars - inputnull if chars is null.public static byte[] hexStrToBytes(CharSequence s, String delim)
The input must consist only of characters
0-9, A-F and a-f as well as the characters
from the optional delimiter.
s - inputdelim - optional delimiter between byte values in the hex string input,
may be null.
If the hex string was created with a delimiter the then same
delimiter must be used here.null if input s is null.HexConversionException - if input CharSequence contains illegal charspublic static byte[] hexStrToBytes(CharSequence s)
The input must consist only of characters 0-9, A-F and
a-f.
s - inputnull if input s is null.HexConversionException - if input CharSequence contains illegal charspublic static void hexToBytesStreaming(InputStream is, OutputStream os, String delim) throws IOException
The input stream must contain only bytes which complies with one of the following:
isHexChar(char).bytesToHexStreaming()
methods of this class.
The input stream is read until end-of-stream or an error occurs. Neither the input stream nor the output stream is closed by this method. You must do this yourself.
is - inputos - outputdelim - HexConversionException - if input stream contains invalid charactersIOException - if reading from the input stream or writing
to the output stream causes an exception.public static void hexToBytesStreaming(Path in, Path out, String delim, OpenOption... outOptions) throws IOException
The input file must contain only bytes which complies with one of the following:
isHexChar(char).bytesToHexStreaming()
methods of this class.in - input fileout - output filedelim - optional delimiter between byte values in the encoded output, may be null.
If the input file uses a delimiter between hex pairs then the same delimiter must be
specified here.outOptions - options specifying how the output file is opened.IOException - if reading from the input file or writing
to the output file causes an exception.HexConversionException - if input file contains invalid characterspublic static boolean isHexStr(String str)
isHexChar(char) method.str - inputstr are hex characters, false otherwise
or if str is null.public static boolean isHexChar(char c)
0-9A-Fa-fc - the character to testCopyright © 2022 lbruun.net. All rights reserved.