Introduction
There are many instances in which we might need to convert a string to a byte array or a byte array to a string. This can be done through the use of character sets. In this lab, we will learn how to do both types of conversions using several different methods.
Using getBytes() method
The getBytes() method of the String class provides a convenient way to obtain a byte array. The String class contains three overloaded versions for the getBytes() method.
String s = "demo!";
byte[] byteArr = s.getBytes();
Using getBytes(String charsetName) method
import java.io.UnsupportedEncodingException;
String s = "demo!";
String namedCharset = "UTF-16";
byte[] byteArr = s.getBytes(namedCharset);
Using getBytes(Charset charset) method
import java.nio.charset.Charset;
import java.util.Arrays;
String s = "demo!";
Charset charset = Charset.forName("UTF-16");
byte[] byteArr = s.getBytes(charset);
Using Charset.encode() method
import java.nio.charset.StandardCharsets;
String s = "demo????????????";
Charset charset = StandardCharsets.ISO_8859_1;
byte[] byteArr = charset.encode(s).array();
Using CharsetEncoder
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
String s = "demo????????????";
CharsetEncoder charsetEncoder = StandardCharsets.ISO_8859_1.newEncoder();
charsetEncoder.onMalformedInput(CodingErrorAction.IGNORE);
charsetEncoder.onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith(new byte[] {-121});
byte[] byteArr = charsetEncoder.encode(CharBuffer.wrap(s)).array();
Using String Constructor
byte[] byteArr = {-2, -1, 0, 100, 0, 101, 0, 109, 0, 111, 0, 33};
String stringFromBytes = new String(byteArr, "UTF-16");
Using Charset Instance
Charset charset = Charset.forName("UTF-16");
byte[] byteArr = {-2, -1, 0, 100, 0, 101, 0, 109, 0, 111, 0, 33};
String stringFromBytes = new String(byteArr, charset);
Using Charset.decode() Method
import java.nio.ByteBuffer;
Charset charset = StandardCharsets.UTF_16;
byte[] byteArr = {-2, -1, 0, 100, 0, 101, 0, 109, 0, 111, 0, 33, -10};
String stringFromBytes = charset.decode(ByteBuffer.wrap(byteArr)).toString();
Using CharsetDecoder
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
CharsetDecoder charsetDecoder = StandardCharsets.UTF_16.newDecoder();
charsetDecoder.onUnmappableCharacter(CodingErrorAction.REPLACE)
.onMalformedInput(CodingErrorAction.REPLACE)
.replaceWith("*");
byte[] byteArr = {-2, -1, 0, 100, 0, 101, 0, 109, 0, 111, 0, 33, -10};
String stringFromBytes = charsetDecoder.decode(ByteBuffer.wrap(byteArr)).toString();
Running the code
Copy the code you want to run into a file such as ~/project/Conversion.java and run the following command in the terminal:
javac Conversion.java && java Conversion
Summary
In conclusion, there are multiple ways to convert a string to a byte array or a byte array to a string. The most convenient way is to use the getBytes() method of the String class to generate a byte array and then use the String class constructor to generate a string from the byte array. Alternatively, we can use Charsets and CharsetEncoders and CharsetDecoders to perform the conversions.



