In this article, we will discuss how to convert String to Byte[] array in Java
1. Byte:
- Size is 1 byte
- Its range is -128 to 127
Q) Why we need to convert String into Byte[] Arrays ?
- Sometimes, it is important to convert String to byte array for base64 computation
2. 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 string representation of 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.
Related Articles:
- Java – String to int conversion – 3 ways
- Java – Integer to String conversion – 6 ways
- Java – String to float conversion – 3 ways
- Java – Float to String conversion – 6 ways
- Java – String to double conversion – 3 ways
- Java – Double to String conversion – 6 ways
- Java – String to long conversion – 3 ways
- Java – Long to String conversion – 6 ways
- Java – String to boolean conversion – 3 ways
- Java – Boolean to String conversion – 6 ways
- Java – String to char conversion
- Java – Character to String conversion – 6 ways
- Java – String to char[] array conversion – 4 ways
- Java – Character[] array to String conversion – 5 ways
- Java – String to byte conversion – 3 ways
- Java – Byte to String conversion – 5 ways
- Java – String to byte[] array conversion
- Java – Byte[] array to String conversion
- Java – String to short conversion – 3 ways
- Java – Short to String conversion – 5 ways
- Java – StringBuffer to String conversion and vice-versa
- Java – StringBuilder to String conversion and vice-versa
- Java – String to Date conversion
- Java – Date to String conversion
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/8/docs/api/java/lang/Byte.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/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 !!