Monday, 12 September 2016

Converting string to bytearray - Java Program

This post shows how String can be converted to byte array and vice versa.

String class has getBytes() method which can be used to convert String to byte array.

getBytes() - Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

There are two other variants of getBytes() method in order to provide an encoding for String.

  • getBytes(Charset charset)
  • getBytes(String charsetName)

Java Program

import java.util.Arrays;

public class StringToByte {

 public static void main(String[] args) {
  String str = "Example String";
  byte[] b = str.getBytes();
  System.out.println("Array " + b);
  System.out.println("Array as String" + Arrays.toString(b));
 }

}

Output

Array [B@2a139a55
Array as String[69, 120, 97, 109, 112, 108, 101, 32, 83, 116, 114, 105, 110, 103]

As you can see here printing the byte array as it is gives the memory address so used Arrays.toString in order to print the array.

Conversion with encoding

Suppose you want to use “UTF-8” encoding then it can be done in 3 ways.

String str = "Example String";
byte[] b;
try {
 b = str.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
}
b = str.getBytes(Charset.forName("UTF-8"));
b = str.getBytes(StandardCharsets.UTF_8);

Using str.getBytes("UTF-8") will require to enclose it in a try-catch block as UnsupportedEncodingException is thrown. To avoid that you can use str.getBytes(Charset.forName("UTF-8")) Java 7 and above you can also use str.getBytes(StandardCharsets.UTF_8);

Converting byte array to String

String class has a constructor which takes byte array as an argument. Using that you can get the String from a byte array.

String(byte[] bytes) - Constructs a new String by decoding the specified array of bytes using the platform's default charset.

If you want to provide a specific encoding then you can use the following constructor -

String(byte[] bytes, Charset charset) - Constructs a new String by decoding the specified array of bytes using the specified charset.

Example code

public class StringToByte {

 public static void main(String[] args) {
  String str = "Example String";
  // converting to byte array
  byte[] b = str.getBytes();
  
  // Getting the string from a byte array
  String s = new String (b);
  System.out.println("String - " + s);
 }
}

Output

String - Example String

That's all for this topic Converting string to bytearray - Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Converting string to int - Java Program
  2. How to find the longest palindrome in the given String
  3. How to find all the permutations of the given String
  4. Check if given strings are anagram or not
  5. How to add double quotes to a String

You may also like -

>>>Go to Java Programs page

No comments:

Post a Comment