Learn to convert byte[] array to String and convert String to byte[] array in Java with examples. Conversion between byte array and string may be used in many cases including IO operations, generate secure hashes etc.
1. Java convert byte[] array to String
1.1. String constructor – new String(byte[])
To convert a byte array to String
, you can use String
class constructor with byte[]
as constructor argument.
byte[] bytes = "hello world".getBytes(); //Convert byte[] to String String s = new String(bytes); System.out.println(s);
Output:
hello world
1.2. Base64 class in Java 8
Since Java 8, we have Base64 class is available.
As you might be aware of – Base64 is a way to encode binary data, while UTF-8 and UTF-16 are ways to encode Unicode text data. So if you need to encode arbitrary binary data as text, Base64 is the way to go.
byte[] bytes = "hello world".getBytes(); //Convert byte[] to String String s = Base64.getEncoder().encodeToString(bytes); System.out.println(s);
Output:
hello world
2. Java convert String to byte[]
2.1. String.getBytes() method
To convert from string to byte array, use String.getBytes() method. Please note that this method uses the platform’s default charset.
//String String string = "howtodoinjava.com"; //Convert string to byte[] byte[] bytes = string.getBytes();
2.2. Base64 class in Java 8
Base64.getDecoder().decode() method converts a string to byte array.
//String String string = "howtodoinjava.com"; //Base64 Decoded byte[] bytes = Base64.getDecoder().decode(string);
3. Summary
We should focus on type of input data when working with conversion between byte[] array and String in Java.
- Use String class when you input data is string or text content.
- Use Base64 class when you input data is byte array.
Drop me your questions in comments section.
Happy Learning !!
Arjit
How do we convert byte[] to JSONObject ?
Matheus Baltazar
Hello,
With this method, can I encode a jpg picture byte array to a string? Because that way would be easier to store on database.
If yes, what about the reverse process? Can I encode this jpg picture string to a byte array? That’s because I’m afraid of losing some picture metadata, like EXIF and etc…
Lokesh Gupta
I will not suggest to do so.
Deepak
I have converted an image having metadata(such as geolocation,date created ,manufacturer and mode) to a Bytestream, but While constructing back the image from bytestream I am losing the above metadata, can you please tell me how I can construct an Image from bytestream, without losing its metadata
gbdcool
I’m wondering how “hello world” string got converted to “howtodoinjava.com” which you printed in console?
When String constructor without Charset argument, Sonar complains saying “Classes and methods that rely on the default system encoding should not be used”.
Lokesh Gupta
Thanks for noticing and reporting this typo error.
You can silent the sonar warning using string constructor which accepts charset, if you want to. I prefer to use default charset which is available even if don’t specify it.
Jan
Prefer
and
to get bytes in correct encoding. Otherwise tools like findbugs will blame you!
Lokesh Gupta
That’s interesting point. Thanks for sharing !!