-
Converts a string to a byte array, using the default encoding of the current project:
String s = "Hello abc";
Byte[] B1 = S.getbytes ();
-
To convert a string in the "GBK" format:
byte[] B2 = s.getbytes ("GBK");
-
To convert a string in the "Utf-8" format:
byte[] B3 = s.getbytes ("Utf-8");
-
To convert a string in "utf-16be" format, UTF-16BE is the encoded format of Java:
byte[] B4 = s.getbytes ("Utf-16be");
-
When a sequence of bytes (a byte array) is a certain encoding, it is also encoded when it is converted to a string, otherwise garbled.
-
Use the default encoding transformation for the current project:
String str1 = new string (b4);
-
Transform using the specified encoding:
String str2 = new String (B4, "utf-16be");
-
Convert bytes to int, shown in 16 binary, repeat 0 and F before last year:
System.out.print (integer.tohexstring (b & 0xff) + "");
-
Bytes encoded in "GBK": A Chinese character occupies two bytes, and an English letter takes up one byte.
-
Bytes encoded in "Utf-8": A Chinese character occupies three bytes, and an English letter takes up one byte.
-
Bytes encoded in "UTF-16BE": one Chinese character accounts for two bytes and one English letter is two bytes.
-
The file encoding for Java is: byte byte byte
-
Text files (. txt) created in the Chinese version of the Windows system are saved in ANSI encoding and can be selected with the Save As action
This article is from the "Personal notes" blog, so be sure to keep this source http://mobileself.blog.51cto.com/4350932/1738721
Java File Encoding Example