GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java HexFormat - formatting and parsing hexadecimal strings in Java Java HexFormatLast modified: July 14, 2026
This Java HexFormat tutorial shows how to format and parse hexadecimal strings
in Java using the Hexadecimal
Hexadecimal (hex) is a base-16 number system that uses
sixteen distinct symbols: the digits
In Java, hexadecimal strings are often used to represent binary data such as
file contents, network packets, hash values, or encryption keys. Prior to Java
17, developers had to write custom logic or rely on third-party libraries to
convert between bytes and hex strings. The Creating HexFormat
The HexFormat hf = HexFormat.of();
HexFormat hfUpper = HexFormat.of().withUpperCase();
HexFormat hfDelim = HexFormat.ofDelimiter(":");
Formatting byte arrays to hex strings
The Main.java
import java.util.HexFormat;
void main() {
byte[] data = {0x4A, 0x61, 0x76, 0x61, 0x21};
HexFormat hf = HexFormat.of();
String hex = hf.formatHex(data);
System.out.println(hex);
}
We create a byte array containing the ASCII values of the word "Java!" and format it as a hexadecimal string. byte[] data = {0x4A, 0x61, 0x76, 0x61, 0x21};
The byte array holds the bytes for the characters J, a, v, a, ! in hexadecimal literal notation. HexFormat hf = HexFormat.of(); String hex = hf.formatHex(data);
A default $ java Main.java 4a61766121 Formatting a range of bytes
The Main.java
import java.util.HexFormat;
void main() {
byte[] data = {0x4A, 0x61, 0x76, 0x61, 0x20,
0x63, 0x6F, 0x64, 0x65};
HexFormat hf = HexFormat.of();
String hex = hf.formatHex(data, 5, 9);
System.out.println(hex);
}
This example formats bytes from index 5 (inclusive) to 9 (exclusive) of the data array. String hex = hf.formatHex(data, 6, 11);
We call $ java Main.java 636f6465 Uppercase hex formatting
To produce uppercase hexadecimal digits, we use the Main.java
import java.util.HexFormat;
void main() {
byte[] data = {0x4A, 0x61, 0x76, 0x61, 0x21};
HexFormat hf = HexFormat.of().withUpperCase();
String hex = hf.formatHex(data);
System.out.println(hex);
}
The $ java Main.java 4A61766121 HexFormat with delimiter
We can configure a delimiter that is inserted between each byte in the formatted
output using Main.java
import java.util.HexFormat;
void main() {
byte[] data = {0x4A, 0x61, 0x76, 0x61, 0x21};
HexFormat hf = HexFormat.ofDelimiter(" ");
String hex = hf.formatHex(data);
System.out.println(hex);
HexFormat hf2 = HexFormat.of().withDelimiter(":").withUpperCase();
String hex2 = hf2.formatHex(data);
System.out.println(hex2);
}
We create HexFormat instances with delimiters. HexFormat hf = HexFormat.ofDelimiter(" ");
String hex = hf.formatHex(data);
The HexFormat hf2 = HexFormat.of().withDelimiter(":").withUpperCase();
String hex2 = hf2.formatHex(data);
We chain $ java Main.java 4a 61 76 61 21 4A:61:76:61:21 HexFormat with prefix and suffix
The Main.java
import java.util.HexFormat;
void main() {
byte[] data = {0x00, 0x01, 0x02, (byte) 0xFF};
HexFormat hf = HexFormat.of()
.withPrefix("0x")
.withDelimiter(", ")
.withUpperCase();
String hex = hf.formatHex(data);
System.out.println(hex);
}
This example configures a HexFormat that prefixes each byte with
$ java Main.java 0x00, 0x01, 0x02, 0xFF Parsing hex strings to byte arrays
The Main.java
import java.util.HexFormat;
import java.util.Arrays;
void main() {
String hex = "48656C6C6F";
HexFormat hf = HexFormat.of().withUpperCase();
byte[] data = hf.parseHex(hex);
System.out.println(Arrays.toString(data));
}
We parse an uppercase hexadecimal string back into a byte array. byte[] data = hf.parseHex(hex);
The $ java Main.java [72, 101, 108, 108, 111] Parsing hex strings with delimiter
When parsing, the Main.java
import java.util.HexFormat;
import java.util.Arrays;
void main() {
String hex = "48:65:6C:6C:6F";
HexFormat hf = HexFormat.ofDelimiter(":");
byte[] data = hf.parseHex(hex);
System.out.println(Arrays.toString(data));
}
We parse a colon-delimited hex string. The $ java Main.java [72, 101, 108, 108, 111] Converting a single byte to hex digits
The Main.java
import java.util.HexFormat;
void main() {
HexFormat hf = HexFormat.of();
String hex1 = hf.toHexDigits((byte) 0xAB);
System.out.println(hex1);
String hex2 = hf.toHexDigits((byte) 0x0F);
System.out.println(hex2);
String hex3 = hf.toHexDigits((byte) 255);
System.out.println(hex3);
}
The $ java Main.java ab 0f ff Converting a long value to hex digits
The overloaded Main.java
import java.util.HexFormat;
void main() {
HexFormat hf = HexFormat.of().withUpperCase();
long value = 0xABCD1234EF56L;
String hex = hf.toHexDigits(value);
System.out.println(hex);
}
This example converts a $ java Main.java 0000ABCD1234EF56
The Creating a hex dumpWe can combine formatting options to create a readable hex dump of binary data. Main.java
import java.util.HexFormat;
void main() {
byte[] data = "The quick brown fox jumps over the lazy dog".getBytes();
HexFormat hf = HexFormat.of().withUpperCase().withDelimiter(" ");
for (int i = 0; i < data.length; i += 16) {
int end = Math.min(i + 16, data.length);
String hex = hf.formatHex(data, i, end);
System.out.printf("%04x: %s%n", i, hex);
}
}
This example produces a formatted hex dump with offset addresses and uppercase hex bytes separated by spaces. $ java Main.java 0000: 54 68 65 20 71 75 69 63 6B 20 62 72 6F 77 6E 20 0010: 66 6F 78 20 6A 75 6D 70 73 20 6F 76 65 72 20 74 0020: 68 65 20 6C 61 7A 79 20 64 6F 67 Parsing hex string to long
The Main.java
import java.util.HexFormat;
void main() {
HexFormat hf = HexFormat.of();
long val1 = hf.fromHexDigits("FF");
System.out.println(val1);
long val2 = hf.fromHexDigits("1A3F");
System.out.println(val2);
}
The $ java Main.java 255 6719
There is also Validating hex digits
The Main.java
import java.util.HexFormat;
void main() {
char[] chars = {'0', '7', 'A', 'a', 'F', 'f', 'G', 'z'};
for (char ch : chars) {
boolean isHex = HexFormat.isHexDigit(ch);
if (isHex) {
int val = HexFormat.fromDigit(ch);
System.out.printf("'%s' is a hex digit with value %d%n", ch, val);
} else {
System.out.printf("'%s' is NOT a hex digit%n", ch);
}
}
}
We test several characters with the static utility methods. boolean isHex = HexFormat.isHexDigit(ch);
The int val = HexFormat.fromDigit(ch);
The $ java Main.java '0' is a hex digit with value 0 '7' is a hex digit with value 7 'A' is a hex digit with value 10 'a' is a hex digit with value 10 'F' is a hex digit with value 15 'f' is a hex digit with value 15 'G' is NOT a hex digit 'z' is NOT a hex digit Formatting a colour codeHexFormat is useful for generating hexadecimal colour codes used in web design. Main.java
import java.util.HexFormat;
void main() {
byte r = (byte) 0x4A;
byte g = (byte) 0x90;
byte b = (byte) 0xD9;
HexFormat hf = HexFormat.of().withUpperCase().withPrefix("#")
.withDelimiter("");
String hex = hf.formatHex(new byte[]{r, g, b});
System.out.println(hex);
}
This example formats RGB byte values into a web colour code. HexFormat hf = HexFormat.of().withUpperCase().withPrefix("#")
.withDelimiter("");
We set the prefix to $ java Main.java #4A90D9 Formatting an MD5 hashThe following example computes an MD5 hash of a string and formats the resulting byte array as a hexadecimal string. Main.java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
void main() throws NoSuchAlgorithmException {
String msg = "Hello there!";
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(msg.getBytes());
HexFormat hf = HexFormat.of().withLowerCase();
String hex = hf.formatHex(digest);
System.out.println(hex);
}
We compute an MD5 digest and format it as a 32-character hexadecimal string.
Warning: MD5 is a weak hash algorithm and should not be used
for hashing passwords or in security-sensitive contexts. It is still commonly
used for non-security tasks such as verifying file integrity, generating
checksums, or checking identity against known values. This example is for
demonstration purposes only.
$ java Main.java a77b55332699835c035957df17630d28 SourceJava HexFormat - Language Reference In this article, we have worked with Java HexFormat. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than eight years of experience in teaching programming. View all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |