Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 897

Converting String to Byte Array - Java Program

$
0
0

In this post we'll see a Java program to convert a String to byte array and vice versa.

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

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 gives the memory address so used Arrays.toString in order to print the array values.

Conversion of string to byte array 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") method to convert String to byte array 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")) method. Java 7 and above you can also use str.getBytes(StandardCharsets.UTF_8);

Converting byte array to String in Java

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 Java code to convert byte array to String


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 Byte Array - 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 - Java Program
  5. How to Add Double Quotes to a String - Java Program

You may also like -

>>>Go to Java Programs Page


Viewing all articles
Browse latest Browse all 897

Trending Articles