Java String to Byte Array: String getBytes() Method Tutorial

In this section, we will learn what the String getBytes() method is and how to use it in Java.

What is Java String getBytes() Method?

The Java String getBytes() method is used to get the bytes representation of a string value in a form of an array.

Encoding a character into bytes needs a scheme which is called character encoding scheme. There are multiple types of encoding scheme as you’ll see them later in this section.

Java getBytes() Method Syntax:

public byte[] getBytes()  
public byte[] getBytes(Charset charset)  
public byte[] getBytes(String charsetName)throws UnsupportedEncodingException  

getBytes() Method Parameters:

This method is overloaded and has three variants. The first one uses the platform’s default character encoding scheme, so we don’t need to set any value as the argument.

The other two variants take one argument, and of course, that is the character encoding scheme.

There are multiple character encoding scheme and if we know the name of the target encoding scheme, we can directly use the name and put it as the argument of the `getBytes()` method.

Here’re some of the known character-encoding schemes:

  • US-ASCII
  • UTF-8
  • UTF-16
  • UTF-32

Also, the `StandardCharsets` class has multiple static attributes of type `Charset` and each of these attributes represents an encoding scheme in which can be used as the argument to this method.

getBytes() Method Return Value:

The return value of this method is an array byte.

getBytes() Method Exceptions:

If we use a wrong type of encoding to this method, it will throw the exception: UnsupportedEncodingException

Example: using String getBytes() method

import java.nio.charset.StandardCharsets;

public class Simple {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String s1 = "Hello";
        byte [] bytes = s1.getBytes(StandardCharsets.US_ASCII);
        printBytes(bytes);
        bytes = s1.getBytes("UTF-32");
        printBytes(bytes);
        String fin = new String(bytes, "UTF-32");
        System.out.println(fin);
    }
    public static void printBytes(byte[] bytes){
        for(byte i: bytes){
            System.out.print(i+", ");
        }
        System.out.println("\n*****");
    }
}

Output:

72, 101, 108, 108, 111,

*****

0, 0, 0, 72, 0, 0, 0, 101, 0, 0, 0, 108, 0, 0, 0, 108, 0, 0, 0, 111,

*****

Hello

Note: we can use array bytes and turn them into string. But the important note is to remember what the character encoding scheme of that array of bytes is in order to decode the bytes correctly.

Take a look at this statement of the example above:

String fin = new String(bytes, "UTF-32");

Here, we know that the `bytes` array is encoded with `UTF-32` scheme. So when we’ve created a string object of this array, we also set the decoding scheme as the second argument to the constructor so that it knows how to decode the bytes.

Byte to String in Java

As you saw in the last example, if you have a byte or an array of bytes and want to convert it into a string value, we can use the String constructor for it.

Basically, one of the variants of this constructor takes two arguments:

  • The first argument is a reference to the target bytes that we want to convert.
  • The second argument is the decoding scheme to be applied on the target bytes.

The return value of calling this constructor is the string value that we want from the bytes.

Example: converting byte array to String

import java.nio.charset.StandardCharsets;

public class Simple {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String s1 = "Hello";
        byte [] bytes = s1.getBytes(StandardCharsets.US_ASCII);
        String fin = new String(bytes, "UTF-32");
        System.out.println(fin);
    }
}

Output:

Hello

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies