HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Array / Convert byte[] Array to String and Vice-versa

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?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Arjit

    December 23, 2019

    How do we convert byte[] to JSONObject ?

  2. Matheus Baltazar

    September 12, 2019

    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

      September 12, 2019

      I will not suggest to do so.

    • Deepak

      January 23, 2020

      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

  3. gbdcool

    April 4, 2019

    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

      April 5, 2019

      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.

  4. Jan

    June 7, 2017

    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!

    • Lokesh Gupta

      June 7, 2017

      That’s interesting point. Thanks for sharing !!

Comments are closed on this article!

Search Tutorials

Java Array

  • Java – Array Introduction
  • Java – Print Array
  • Java – Print 2D Array
  • Java – Copy Array
  • Java – Copy Array Range
  • Java – Clone Array
  • Java – Array Deep Copy
  • Java – String to String[]
  • Java – byte[] to String
  • Java – String to byte[]
  • Java – Array Union
  • Java – Array Intersection
  • Array – Remove duplicate elements

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)