JDK 17: Hexadecimal Formatting and Parsing
Here's the skinny on hexadecimal in Java 17.
Join the DZone community and get the full member experience.Join For Free
Build 3 of JDK 17 Early Access Builds includes the implementation for JDK-8251989 ("Hex formatting and parsing utility"). This newly introduced functionality for parsing and formatting hexadecimal values is encapsulated in the new class
java.util.HexFormat and is the subject of this post.
Running javap against the new
java.util.HexFormat class provides an easy way to see an overview of its API. The following output is generated from running
The javap-generated listing shown above indicates that there are two
static factory methods for obtaining an instance of
HexFormat.ofDelimiter(String). Both of these factory methods specify instances of
HexFormat with "preset parameters." The remainder of the
public methods are instance methods that are generally used for one of five categories of action:
- Instruct the
HexFormatinstance to apply different parameters than the preset parameters the instance was instantiated with
- Indicate configured parameters of
- Convert to and from hexadecimal representations
- Indicate characteristics of characters and character sequences
The class-level Javadoc for
HexFormat's summarizes the purposes of the
HexFormat class in a single sentence: "
HexFormat converts between bytes and chars and hex-encoded strings which may include additional formatting markup such as prefixes, suffixes, and delimiters." That class-level Javadoc-based documentation also provides useful examples of applying the
HexFormat class to covert between these types and to apply prefixes, suffixes, and delimiters. The class-level documentation further explains that the
HexFormat class is "immutable and threadsafe" and is a " value-based class."
In the last version of
HexFormat class source code that I saw, it was advertising "@since 16", which is one piece of evidence of the work that has been invested in this class in terms of implementation, review, and incorporated feedback (the 33 commits is another piece of evidence). The official release of
HexFormat is actually JDK 17, but the JDK 17 Early Access API Documentation still shows "@since 16" as of this writing.
In this post, I provide some simple examples of applying
HexFormat and these code listings are available on GitHub. Fortunately, the class-level Javadoc-based API documentation provides really good examples of applying
HexFormat. I like it when classes' Javadoc shows examples of how to apply those classes and the
HexFormat documentation does a good job of covering many aspects of using that class. My examples will cover a smaller portion of the class's API and is meant solely as an introduction to the basic availability of this class.
Acquiring an Instance of
There are two
static methods for acquiring an instance of
HexFormat and one of those is demonstrated here:
withUpperCase() method instructs the instance of
HexFormat to "use uppercase hexadecimal characters" ("0-9", "A-F").
Converting Integers to Hexadecimal
The code snippet shown next demonstrates use of
When the above code snippet is executed, the output looks like this:
The following code demonstrates
Here is the output from running the above code snippet:
HexFormat class provides an overriden version of the
Object.toString() method and this is demonstrated in the following code snippet and corresponding output from running that code snippet.
HexFormat.toString(): uppercase: true, delimiter: "", prefix: "", suffix: ""
Other Examples of
The Javadoc-based class-level documentation for
HexFormat contains more examples of how to apply this class. The examples demonstrate instantiation methods
HexFormat.ofDelimiter(String); demonstrate utility methods
parseHex(String); and demonstrate instance specialization methods
withPrefix(String). I like that the latter examples are "realistic" examples of how operations might be used in practical situations (such as with byte fingerprints).
JDK Uses of
The JDK and its tests already use
HexFormat. The following are some examples of this.
Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.