A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-nio-charset-charset-class-in-java/ below:

java.nio.charset.Charset Class in Java - GeeksforGeeks

java.nio.charset.Charset Class in Java

Last Updated : 07 Nov, 2023

In Java, Charset is a mapping technique used in Java to map the 16-bit Unicode sequence and sequences of bytes. It is also used to encode and decode the string data text into different character encoding. It comes under java.nio.charset.Charset package.

The charset must begin with a number or letter. Every charset can decode and encode. For constructing a map that contains every charset, support is available in JVM(Java Virtual Machine).

Methods of Charset

Creates a new decoder

Creates a new encoder

Returns the canonical name

Returns an array of aliases

Tests whether this charset is supported by the current Java virtual machine

Classes of Charset

this is a named mapping between characters and bytes

this class decodes bytes into characters

this class encodes character into bytes

it is a description of the result state of a coder

it detects coding errors and take error action

Standard charsets

7 bit ASCII characters. Represents the basic English alphabet and some control characters.

ISO Latin Alphabet No. 1 that covers the Latin script and some common symbols.

8-bit UCS Transformation which consists of most of the characters(from different languages).

16-bit UCS Transformation Format in this characters are encoded using big-endian byte

16-bit UCS Transformation Format in this characters are encoded using little-endian byte order.

16-bit UCS Transformation Format this is often used for internal text processing.

Using a charset Encoding a string into sequence of bytes

Encoded String into a sequence of bytes using the given charset, storing the result into a new byte array.

public byte[] getBytes(Charset charset);
Example:
java.nio.charset.Charset charset = java.nio.charset.Charset.forName("ASCII");
byte[] byteArray = "Hi".getBytes(charset);
Java Charset Example Java
public class Main
{
    public static void main(String[] args)
    {
        String s= "GFG";

        java.nio.charset.Charset charSet = java.nio.charset.Charset.forName("ASCII");
        
        byte[] byteArr= s.getBytes(charSet);
        System.out.println("byteArr of \"GFG\" with charsetName \"ASCII\" = " + byteArr);
        for (byte a : byteArr)
        {
            System.out.println(a);
        }

    }
}

Output
byteArr of "GFG" with charsetName "ASCII" = [B@3af49f1c
71
70
71




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4