- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert byte Array to Hex String in Java
The following is our byte array.
byte[] b = new byte[]{'p', 'q', 'r'};
We have created a custom method “display” here and passed the byte array value. The same method converts byte array to hex string.
public static String display(byte[] b1) { StringBuilder strBuilder = new StringBuilder(); for(byte val : b1) { strBuilder.append(String.format("%02x", val&0xff)); } return strBuilder.toString(); }
Let us see the entire example now.
Example
public class Demo { public static void main(String args[]) { byte[] b = new byte[]{'p', 'q', 'r'}; /* byte array cannot be displayed as String because it may have non-printable characters e.g. 0 is NUL, 5 is ENQ in ASCII format */ String str = new String(b); System.out.println(str); // byte array to Hex String System.out.println("Byte array to Hex String = " + display(b)); } public static String display(byte[] b1) { StringBuilder strBuilder = new StringBuilder(); for(byte val : b1) { strBuilder.append(String.format("%02x", val&0xff)); } return strBuilder.toString(); } }
Output
pqr Byte array to Hex String = 707172
- Related Articles
- Convert Hex String to byte Array in Java
- How to convert hex string to byte Array in Java?
- How to convert a byte array to hex string in Java?
- How to convert a byte array to a hex string in Java?
- Java Program to convert byte[] array to String
- Java Program to convert String to byte array
- Convert byte to String in Java
- Convert Integer to Hex String in Java
- Java Program to convert byte to string
- Java Program to convert string to byte
- How to convert byte array to string in C#?
- How to convert BLOB to Byte Array in java?
- How to convert Byte Array to Image in java?
- How to convert Image to Byte Array in java?
- How to convert InputStream to byte array in Java?

Advertisements