HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Array / Java byte[] to String Example

Java byte[] to String Example

While working with encryption algorithms, you may get encrypted text as byte[], which later need to be converted to String for comparison purpose. Converting byte[] to String in java is pretty much straightforward and easy. You need to use String class constructor String(byte[] bytes) or String(byte[] bytes, String charsetName) and you will get String represenation of byte array content.

String str = new String( byteArray );

//or

String str = new String( byteArray , charset );

Convert byte[] to String

Let’s look at an example for more clarity. I will convert a String to byte array, and then convert back to String – then we will compare both strings and they should match.

String name = "howtodoinjava.com";
		
byte[] byteArray = name.getBytes();

String str = new String(byteArray);
String strWithCharset = new String(byteArray, Charset.defaultCharset());

System.out.println("Original String: "+ name );
System.out.println("Obtained String: "+ str );
System.out.println("Obtained String: "+ strWithCharset );

Output:

Original String: howtodoinjava.com
Obtained String: howtodoinjava.com
Obtained String: howtodoinjava.com

It’s easy but very handy when you how to do it.

Happy Learning !!

Was this post helpful?

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

About Lokesh Gupta

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

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

  • Sealed Classes and Interfaces