How can I convert a byte array to hexadecimal in Java?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Converting a byte array to a hexadecimal string in Java is a common requirement when dealing with binary data. Hexadecimal representation is convenient for debugging, logging, or displaying bytes in a human-readable format since it condenses each byte into two characters. This article provides a comprehensive guide on how to efficiently accomplish this task using Java, detailing the various approaches and their intricacies.
Understanding Byte Arrays and Hexadecimal
Before delving into code examples, let's clarify what byte arrays and hexadecimal formats are:
- Byte Array: An array of type
bytein Java represents a collection of 8-bit binary data. Each element can hold a value between -128 and 127. - Hexadecimal Representation: This is a base-16 numbering system using sixteen distinct symbols, typically 0-9 and A-F, to represent values.
Method 1: Using StringBuilder and Bitwise Operations
The most explicit method to convert a byte array to a hexadecimal string is by using a loop in conjunction with bitwise operations. Here's how it works:
Explanation:
- Masking with
0xFF: This operation converts the signed byte to a positive integer, effectively zero-filling the leftmost bits. - Padding with Zero: The result of
Integer.toHexString()may produce a single character for values less than 16. Padding ensures two-character width for each byte.
Method 2: Using DatatypesConverter (JDK 1.6 to 1.8)
For Java 6 and above, javax.xml.bind.DatatypeConverter offers a simpler solution. However, this class was deprecated in Java 9 and later removed in newer releases.
Explanation:
This approach abstracts the conversion behind a simple method call, providing a convenient yet deprecated solution for specific JDK versions.
Method 3: Using Commons Codec Library
Apache Commons Codec library simplifies the conversion with a built-in utility:
- Add the Dependency:
- For Maven, include:
- Converting Byte Array to Hex:
Explanation:
This method expedites development by leveraging a robust external library widely used for encoding and decoding operations.
Performance Considerations
Each method has its pros and cons:
- Manual Conversion offers full control and understanding, and it's lightweight but can be verbose.
DatatypeConverterprovides ease of use but lacks support in newer Java versions due to deprecation.- Commons Codec gives a neat and reliable solution at the cost of an external dependency, especially beneficial in projects already using this library.
Key Points Summary
| Method | Pros | Cons |
Manually using StringBuilder
and Bitwise Operations | Full control, No dependencies | Verbose, Manual padding needed |
DatatypeConverter | Simple, Built-in (for JDK <= 1.8) | Deprecated post JDK 8 |
| Commons Codec (Apache) | Simple, Reliable | External dependency required |
Conclusion
Converting a byte array to a hexadecimal string in Java can be accomplished in several ways, each with its own use cases and constraints. Depending on your specific requirements and project setup (like existing dependencies or Java version), you can choose the method that best fits your needs. It's always prudent to consider both performance implications and future maintainability when selecting an approach.
Related reading
- How can I convert a comma-separated string to an array?
- How can I convert a dictionary into a list of tuples?
- How can I convert comma separated string into a Listint
- How can I convert each item in the list to string, for the purpose of joining them?
- How can I convert a long to int in Java?
- How can I convert byte size into a human-readable format in Java?
- How can I convert int to Integer in Java?
- How can I convert JSON to a HashMap using Gson?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.