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); } } } |
byteArr of "GFG" with charsetName "ASCII" = [B@3af49f1c 71 70 71
java.nio.charset.Charset Class in Java
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).