Encode a String into a byte array in Java
This post will discuss how to encode a string into a byte array in Java.
There are several ways to encode a string into a byte array in Java, which is the process of translating the sequence of characters in the string into a sequence of bytes using a specific character set. Here are some possible methods:
1. Using getBytes() method
We can encode the string into a sequence of bytes using the String.getBytes() method, which is an overloaded method that can encode a string using the platform’s default character set. This method returns a byte array that contains the encoded string. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; public class Main { public static void main(String[] args) { String str = "Java"; // encode the string using the platform's default character set byte[] bytes = str.getBytes(); System.out.println(Arrays.toString(bytes)); // [74, 97, 118, 97] } } |
If our string uses a different encoding than the default charset, specify that character encoding in the second argument to the String.getBytes() method. The second argument can accept a named character set or a provided character set.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.nio.charset.StandardCharsets; import java.util.Arrays; public class Main { public static void main(String[] args) { String str = "Java"; // encode a string using the UTF_16 character set byte[] bytes = str.getBytes(StandardCharsets.UTF_16); System.out.println(Arrays.toString(bytes)); // [-2, -1, 0, 74, 0, 97, 0, 118, 0, 97] } } |
To convert a byte array back to a string, we can pass the byte array to the String() constructor with the charset used for encoding. Here’s an example of this approach:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.nio.charset.StandardCharsets; public class Main { public static void main(String[] args) { String str = "Java"; byte[] bytes = str.getBytes(StandardCharsets.UTF_8); String decodedStr = new String(bytes, StandardCharsets.UTF_8); System.out.println(decodedStr); // Java } } |
2. Using Apache Commons Library
Another option to encode a string into a byte array in Java is to use the StringUtils.getBytes(String, Charset) utility method from the org.apache.commons.lang3 package. This method converts a string to a byte array using the specified character set. This internally calls the String.getBytes(Charset) method, but handles null input gracefully. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.apache.commons.lang3.StringUtils; import java.nio.charset.Charset; import java.util.Arrays; public class Main { public static void main(String[] args) { String str = "Java"; byte[] bytes = StringUtils.getBytes(str, Charset.defaultCharset()); System.out.println(Arrays.toString(bytes)); // [74, 97, 118, 97] } } |
We can also leverage the Apache Commons Codec StringUtils class to convert the String to and from bytes using the respective getBytes*() and newString*() utility methods from the org.apache.commons.codec.binary package.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import org.apache.commons.codec.binary.StringUtils; import java.util.Arrays; public class Main { public static void main(String[] args) { String str = "Java"; byte[] bytes = StringUtils.getBytesUtf8(str); System.out.println(Arrays.toString(bytes)); // [74, 97, 118, 97] String decodedStr = StringUtils.newStringUtf8(bytes); System.out.println(decodedStr); // Java } } |
3. Using Charset.encode() method
Use the Charset.encode() method, which is a method that can encode a string using a specific character set. This method returns a ByteBuffer that contains the encoded string. We can then convert the ByteBuffer to a byte array using the array() method. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.Arrays; public class Main { public static void main(String[] args) { String str = "Java"; // encode a string using a specific character set ByteBuffer buffer = StandardCharsets.UTF_8.encode(str); // convert the ByteBuffer to a byte array byte[] bytes = buffer.array(); System.out.println(Arrays.toString(bytes)); // [74, 97, 118, 97] } } |
That’s all about encoding a string into a byte array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)