English letters:
Number of bytes: 1; encoding: gb2312
Number of bytes: 1; encoding: GBK
Number of bytes: 1; encoding: gb18030
Byte Count: 1; encoding: ISO-8859-1
Byte Count: 1; encoding: UTF-8
Byte Count: 4; encoding: UTF-16
Byte Count: 2; encoding: UTF-16BE
Byte Count: 2; encoding: UTF-16LE
Chinese characters:
Number of bytes: 2; encoding: gb2312
Number of bytes: 2; encoding: GBK
Number of bytes: 2; encoding: gb18030
Byte Count: 1; encoding: ISO-8859-1
3; encoding: UTF-8
Byte Count: 4; encoding: UTF-16
Byte Count: 2; encoding: UTF-16BE
Byte Count: 2; encoding: UTF-16LE
You can useCodeSee the above results:
Import java. Io. unsupportedencodingexception;
Public class encodingtest {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
String en = "";
String CH = "persons ";
System. Out. println ("English letter:" + en );
Printbytelength (EN, "gb2312 ");
Printbytelength (EN, "GBK ");
Printbytelength (EN, "gb18030 ");
Printbytelength (EN, "ISO-8859-1 ");
Printbytelength (EN, "UTF-8 ");
Printbytelength (EN, "UTF-16 ");
Printbytelength (EN, "UTF-16BE ");
Printbytelength (EN, "UTF-16LE ");
System. Out. println ();
System. Out. println ("Chinese characters:" + CH );
Printbytelength (CH, "gb2312 ");
Printbytelength (CH, "GBK ");
Printbytelength (CH, "gb18030 ");
Printbytelength (CH, "ISO-8859-1 ");
Printbytelength (CH, "UTF-8 ");
Printbytelength (CH, "UTF-16 ");
Printbytelength (CH, "UTF-16BE ");
Printbytelength (CH, "UTF-16LE ");
}
/**
* Print the number of bytes occupied by Java strings in different character sets
*
* @ Param Str
* String to be operated
* @ Param encodingname
* Character Set Name
**/
Public static void printbytelength (string STR, string encodingname ){
System. Out. Print ("Bytes :");
Try {
System. Out. Print (Str. getbytes (encodingname). Length );
} Catch (unsupportedencodingexception e ){
E. printstacktrace ();
}
System. Out. println ("; encoding:" + encodingname );
}
}