Convert byte[] Array to String and Vice-versa

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.

Until it is absolute necessary, DO NOT convert between string and byte array. They both represent different data; and are there to serve specific purposes i.e. strings are for text, byte[] is for binary data.

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.

  1. Use String class when you input data is string or text content.
  2. Use Base64 class when you input data is byte array.

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

8 thoughts on “Convert byte[] Array to String and Vice-versa”

  1. 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…

    • 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

  2. 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”.

  3. Prefer

    public byte[] getBytes(Charset charset)

    and

    public String(byte bytes[], Charset charset)

    to get bytes in correct encoding. Otherwise tools like findbugs will blame you!

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.