Charset decode() in Java

decode(): This method is available in java.nio.charset.Charset class of Java.

Syntax:

CharBuffer java.nio.charset.Charset.decode(ByteBuffer bb)

This method takes one argument of type ByteBuffer as its parameter. This method is a convenient method that decodes bytes in this charset into Unicodecharacters.

Parameters: One parameter is required for this method.

bb: The byte buffer to be decoded.

Returns: A char buffer containing the decoded characters.

For Example:

Charset cs = Charset.forName("UTF-8")

byte array[] = { '1', '2', '4', '6' }

ByteBuffer bb = ByteBuffer.wrap(array)

cs.decode() = > It returns [1, 2, 4, 6].

Approach

Java

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;

public class Charsetdecode {
    public static void main(String[] args) {

      Charset cs = Charset.forName("UTF-8");

      byte array[] = { '1''2''4''6' };

      ByteBuffer bb = ByteBuffer.wrap(array);
      System.out.println(Arrays.toString(cs.decode(bb).array()));

    }
}

Output:

[1, 2, 4, 6]

No comments:

Post a Comment