a shot of dev knowledge

RELATED TAGS

What is StringUtils.toEncodedString() in Java?

toEncodedString() is a staticthe methods in Java that can be called without creating an object of the class. method of the StringUtils class that is used to convert an array of bytes to a string using the given character encoding.

How to import StringUtils

The definition of StringUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the StringUtils class as follows:


import org.apache.commons.lang3.StringUtils;

Syntax

public static String toEncodedString(byte[] bytes_arr, Charset charset)

Parameters

bytes_arr: The sequences of bytes to convert to a string.

charset: The encoding to use for string conversion. If no character set is provided, the default platform encoding is used.

Return value

This method returns a new string based on the encoding provided.

Code

import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        
        // Example 1
        byte[] bytes = new byte[]{72, 101, 108, 108, 79, 45, 69, 68, 85, 99, 97, 116, 105, 118, 101};
        System.out.printf("The output of StringUtils.toCodePoints() for the byte array - '%s' is '%s'", Arrays.toString(bytes), StringUtils.toEncodedString(bytes, StandardCharsets.UTF_8));
        System.out.println();

        // Example 2
        bytes = new byte[0];
        System.out.printf("The output of StringUtils.toCodePoints() for the byte array - '%s' is '%s'", Arrays.toString(bytes), StringUtils.toEncodedString(bytes, StandardCharsets.UTF_8));
        System.out.println();

        // Example 3
        bytes = null;
        System.out.printf("The output of StringUtils.toCodePoints() for the byte array - '%s' is '%s'", Arrays.toString(bytes), StringUtils.toEncodedString(bytes, StandardCharsets.UTF_8));
        System.out.println();

    }
}

Output

The output of the code will be as follows:


The output of StringUtils.toCodePoints() for the byte array - '[72, 101, 108, 108, 79, 45, 69, 68, 85, 99, 97, 116, 105, 118, 101]' is 'HellO-EDUcative'
The output of StringUtils.toCodePoints() for the byte array - '[]' is ''
Exception in thread "main" java.lang.NullPointerException
	at java.base/java.lang.String.<init>(String.java:561)
	at org.apache.commons.lang3.StringUtils.toEncodedString(StringUtils.java:9034)
	at Main.main(Main.java:19)

Explanation

Example 1

  • bytes = [72, 101, 108, 108, 79, 45, 69, 68, 85, 99, 97, 116, 105, 118, 101]
  • encoding = UTF-8

The method returns HellO-EDUcative, where the byte array is converted to a string with UTF-8 as the encoding.

Example 2

  • bytes = []
  • encoding = UTF-8

The method returns "", as the byte array is empty.

Example 3

  • bytes= null
  • encoding = UTF-8

The method throws a NullPointerException exception, as the byte array is null.

RELATED TAGS

RELATED COURSES

View all Courses

Keep Exploring