how to convert Byte[] array to String in Java

In some cases, we have to convert String variable into a Byte array format, for example, JCE encryption. However how do we convert a Byte[] array to String afterward?

Simple toString() function like following code is not working property. It will not display the original text but byte value.

String s = bytes.toString();

In order to convert Byte array into String format correctly, we have to explicitly create a String object and assign the Byte array to it.

String s = new String(bytes);
public class TestByte
{    
	public static void main(String[] argv) {
 
		    String example = "This is an example";
		    byte[] bytes = example.getBytes();
 
		    System.out.println("Text : " + example);
		    System.out.println("Text [Byte Format] : " + bytes);
		    System.out.println("Text [Byte Format] : " + bytes.toString());
 
		    String s = new String(bytes);
		    System.out.println("Text Decryted : " + s);
 
 
	}
}

Output

Text : This is an example
Text [Byte Format] : [B@187aeca
Text [Byte Format] : [B@187aeca
Text Decryted : This is an example
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: compress and decompress String using DeflaterOutputStream and InflaterInputStream in java  |  Stackforum.com()

  • Pingback: compress and decompress String using DeflaterOutputStream and InflaterInputStream in java | StackAnswer.com()

  • Amar

    Thanks for the information. Could you also please let me know how to convert Byte[] (capital B) to String. The above code does not work for the nonprimitive Byte[]. Thank you.

    Regards,
    Amar

  • faeze

    tnx alot

    • Amar

      Thanks for the information. Could you also please let me know how to convert Byte[] (capital B) to String. The above code does not work for the nonprimitive Byte[]. Thank you.

      Regards,
      Amar

    • Amar

      Sorry for posting my comment in your reply section. I am not sure how to delete my reply.

  • rushank

    if we are taking array of string then it will show error….so can i have to do if i want to use string array..
    i want to do this… -> String[] s = bytes.toString();

    bt it will give me erroe

  • mahya

    please tell me a program source code in java that convert text to binary and binary to text.

  • subhash M

    Thank you

  • Karthik SR

    Thanks sir! Saved me several hours. You are awesome!

  • sahil

    Hi!!, Do you have anything on converting a string to Octet String in Java. I tried but couldn’t get anything concrete.

  • Yuvraj

    This is helpful solution…

  • Abhijith

    Thanks man. That helped.

  • Kalyan Kumar

    I am trying to convert bytes into String. I am reading the image data and storing into byte array. then i think bytes length is so much. Is there any limit for bytes length? Will there be anyloss of data when converting bytes into string.

  • Kalyan Kumar

    Dear Mkyong

    I am trying to convert bytes into String. Is there any limit for bytes length? Will there be anyloss of data when converting bytes into string.

  • Diljeet

    Thanks for sharing

  • ogda

    Thank you :)

  • Mic and Rud

    YOU ARE THE MAN!!

  • http://www.thepcwizard.in ThePCWizard

    Thanks, for sharing such simple codes. it helps!

  • saidtak

    The issue I find with this byte string conversion is starting from byte{], convert it to String, then retrieve the original byte{} again!

    The most stable solution I found so far is using sun.misc.BASE64Encoder().encode(myByte) and sun.misc.BASE64Decoder().decodeBuffer(myString) but with a java sun warning!

    Any one with a better solution?

    • saidtak

      byt[] of course!

  • DW

    This example relies on a number of assumptions that are important to understand. You should always specify the encoding when converting bytes. Also, the system.out statements show the string representation of the reference to the byte array, not the byte array itself.

    This example works since both the byte array and converting it back use the same default encoding of the JVM

  • http://gmail.com rohan

    This helped and saved time for a newbie like me :)

  • LM

    oh, thanks a lot. this simple code help me very much.

  • sandy

    This is returning hashcode..not bytes

  • http://þÿ Marty Brouwer

    you are really a good webmaster. The web site loading speed is incredible. It sort of feels that you are doing any distinctive trick. In addition, The contents are masterpiece. you’ve performed a magnificent activity on this subject!

  • http://www.androidsx.com Pablo

    In most cases,

    new String(bytes, "UTF-8");

    is best.

  • Paul

    Thank you so much for this! I looked for quite a while before discovering this simple solution. Really appreciate it!

  • madsci

    The obvious conversions didn’t work, and older jdbc drivers didn’t require this! Was faced with upgrading old code that still works fine and it took a while to realize that byte[] conversion to String had changed. Then once I got that figured out your page simplified the needed correction. Thanks for showing the examples that don’t work along with the ones that do, and the [B@… craziness too so it was easy to find! Big help!
    _M

  • betty

    Thanks a lot for sharing sample codes. It saves me time!

  • vj

    thnx a lot…saved ma day :P