Convert byte array to string in java & string to byte array (sequence of bytes) with example code

The following tutorial tells about how to convert a String to byte array and byte array to String with suitable examples. Byte array may be used as buffer in many programs to store the data in byte format .The following lines of code read data from the input stream to the buffer and the buffer gets printed as readable characters until end of stream.

 InputStream in = new FileInputStream("filename");
  byte[] buf = new byte[1024];
   while ((int l =,0,)) !=-1) {
 System.out.println(new String(buf));

Example from Java Source:

Some of methods which are used to encrypt data (such as doFinal() of Cipher class , digest() method of MessageDigest class ) of JCE API accepts byte array, encrypts the data and returns the byte array as encrypted data . The following lines of code retrieve the sequence of bytes from the original string and passed to the doFinal() method which returns the encrypted sequence of bytes as byte array.


private static byte[] encrypt(String input)  throws Exception, InvalidKeyException, BadPaddingException,    IllegalBlockSizeException {
   cipher.init(Cipher.ENCRYPT_MODE, key); (Assume that cipher object is already created)
   byte[] inputBytes = input.getBytes(); // returns as byte array
   return cipher.doFinal(inputBytes); // returns as encrypted byte array

The following lines of code decrypts the sequence of encrypted bytes and returns a byte array. The byte array needs to be converted to original String that can be readable . 

 private static String decrypt(byte[] encryptionBytes) throws InvalidKeyException,    BadPaddingException,  IllegalBlockSizeException {
   cipher.init(Cipher.DECRYPT_MODE, key);
   byte[] decryptedBytes =   cipher.doFinal(encryptionBytes); (Assume that cipher object is already created)
   String originalString =    new String(decryptedBytes);
   return originalString;

So now let us see that How to convert String to byte array and vice versa.

To convert String to byte array of non- Unicode characters , use getBytes() method of String class:
           String.getBytes() : Encodes this String into a sequence of bytes using the the character encoding (eg. ASCII , …) used by the underlying OS , storing the result into a new byte array.

byte[] inputBytes = input.getBytes(); where input is a string

To convert sequence of bytes to String :

             toString() method of byte array does not help you. Converting byte array to String in Java is simple as one of the String class constructors accepts byte array as an argument. If a byte array contains non-Unicode text, you can convert the text to Unicode with one of the String constructor methods.

  String originalString = new String(decryptedBytes); where decryptedBytes is a byte array.

 Note : When we convert String to byte array, the characters of the string object is stored as 8-bit characters. The conversion of characters from Unicode to 8-bit bytes depends upon the default encoding of your system. When the string contain Unicode characters is converted to 8-bit bytes, upper byte of the Unicode character is discarded, resulting in the ASCII equivalent. In this case, the result of the getBytes() method is unspecified.

 To convert the String object to UTF-8, invoke the getBytes method and specify the appropriate encoding identifier as a parameter.

byte[] bytes_utf8 = input.getBytes(“UTF8”); 

                    –  The above getBytes method returns an array of bytes in UTF-8 format.

To create a String object from an array of non-Unicode bytes, invoke the String constructor with the encoding parameter.

String originalString = new String(bytes_utf8, “UTF8”);


Reference :

Leave a Reply