Have you encountered an API that returns encoded data? How do you decode the data to use in your application? If this is a challenge to you, don’t worry, we’ve got you covered. In this tutorial, you will learn how to convert byte array to String and vice versa in Java.
Using String class and ISO_LATIN_1 character set
ISO_LATIN_1 is an encoding system that contains 198 characters. The bit length of each character in the system is 8 bits. Additionally, you can use the encoding system with any system. Let’s look at an example to understand how you can use it to convert a byte array to a String.
package com.javawhizz; import java.nio.charset.StandardCharsets; import java.util.Arrays; public class Main { public static void main(String[] args) { byte[] bytes = {1, -2, -3, 10, 34}; String stringBytes = new String(bytes, StandardCharsets.ISO_8859_1); System.out.println(stringBytes); System.out.println(Arrays.toString(stringBytes .getBytes(StandardCharsets.ISO_8859_1))); } }
In this example, the byte array holds the data to convert to String. To convert the byte array to String, create a new instance of the String class. Next, pass the byte array and ISO_LATIN_1 as the arguments. you can access the ISO_LATIN_1 character set using the field StandardCharsets.ISO_8859_1
.
To reverse the String to a byte array, call the getBytes()
method and then pass ISO_LATIN_1 as the argument.
If the byte array gets converted to String and vice versa without any errors, you should see the output:
þý " [1, -2, -3, 10, 34]
Using Base64 encoding scheme
Base64 is an encoding scheme used to get encoders and decoders. If you want to know the characters supported by this encoding scheme, you can refer to RFC 4648 and RFC 2045.
The following example shows how to use Base64 to convert a byte array to a String and vice versa.
package com.javawhizz; import java.util.Arrays; import java.util.Base64; public class Main { public static void main(String[] args) { byte[] bytes = {1, -2, -3, 10, 34}; String stringBytes = Base64 .getEncoder() .encodeToString(bytes); System.out.println(stringBytes); byte[] decodedBytes = Base64 .getDecoder() .decode(stringBytes.getBytes()); System.out.println(Arrays.toString(decodedBytes)); } }
To convert the byte array to String using Base64, first, call the getEncoder()
static method. Next, call the encodeToString()
method and pass the byte array as the argument. This will encode the byte array to a String.
To reverse the byte array, call the getDecoder()
static method followed by a call to decode()
method. Chain the method calls together. Next, pass the encoded bytes as the argument of the method. As a result, the method will decode the String to a byte array.
If the byte arrays get encoded to String and vice versa without any errors, you should see the output:
Af79CiI= [1, -2, -3, 10, 34]
Conclusion
In this tutorial, you have learned how to convert a byte array to a String and vice versa in Java. Generally, these are the two common approaches to use when you want to convert a String to a byte array. There are other approaches provided on the internet. Nonetheless, be careful which approach you use as it might not work as expected.
Happy Hacking!
0 Comments