String to Byte[] Array conversion in Java

In this article, we will discuss how to convert String to Byte[] array in Java

 

Byte:

  • Size is 1 byte
  • Its range is -128 to 127

 

Need:

  • Sometimes, it is important to convert String to byte array for base64 computation

 

Converting String to byte[] array using getBytes(); method

  • This method can be used to convert String into primitive byte[] array
  • Note: The value range should be within -128 to 127

Method signature:

public byte[] getBytes(String charsetName);

 

ConvertStringToByteArrayUsingGetBytesMethod.java

package in.bench.resources.string.to.bytes.conversion;

import java.util.Arrays;

public class ConvertStringToByteArrayUsingGetBytesMethod {

	public static void main(String[] args) {

		// String
		String encPassword = "ThisIsMyPassWord";

		// original text
		System.out.println("Original String : " + encPassword);

		// converting String to byte[] array
		byte[] byteArray = encPassword.getBytes();

		// 1. printing in Byte format after conversion
		System.out.println("\nConverted byte[] array in Byte format : " 
				+ byteArray);

		// 2. Returns a string representation of the specified array
		System.out.println("\nConverted byte[] array in Array format : " 
				+ Arrays.toString(byteArray));
	}
}

Output:

Original String : ThisIsMyPassWord

Converted byte[] array in Byte format : [B@1615099

Converted byte[] array in Array format : [84, 104, 105, 115, 73, 115, 77, 121, 80, 97, 115, 115, 87, 111, 114, 100]

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
https://docs.oracle.com/javase/tutorial/java/data/converting.html
https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
https://docs.oracle.com/javase/6/docs/api/java/lang/Byte.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Byte.html
https://docs.oracle.com/javase/8/docs/api/java/lang/Byte.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
http://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html

 

Happy Coding !!
Happy Learning !!

Byte Array to String conversion in Java
Byte to String conversion in Java - 5 ways