Understanding file encoding and file encoding

Source: Internet
Author: User

Understanding file encoding and file encoding

 

  • UTF-16BE: The Chinese Character occupies 2 bytes, the letter occupies 2 bytes.
  • UTF-8: Chinese characters accounted for 3 bytes, letters accounted for 1 byte.
  • GBK: a Chinese character occupies 2 bytes, and a letter occupies 1 byte. The Java project is encoded as GBK by default.
  • ANSI: in a Chinese system, ansi encoding represents GBK encoding.

 

 

Java is double-byte encoding. A single character occupies 2 bytes. Therefore, a character in Java can contain one Chinese character. The default Java project code is GBK, and a Chinese character contains two bytes, which can be exactly put in without garbled characters.

 

When yourByte sequenceThis encoding method is also used if you want to edit the byte sequence into a string. Otherwise, garbled characters may occur.

In the program, the byte sequence into a string, if the encoding method of the byte sequence is not given, the system will use the default encoding method GBK, for example, bytes is a UTF-8 of the byte sequence array, if you want to convert it to the string represented by this byte sequence, you must provide this encoding method:

    String str = new String (bytes, "UTF-8 ");

If not given, it will be converted to a string according to the GBK encoding method, and garbled characters will appear.

 

A text file is actually a byte sequence.

You can view any encoded text in a text file without garbled characters.

If you create a text file directly on a Chinese machine, the text file is ansi encoded (GBK) by default to save the text information.

 

In Eclipse, the encoding format set by the project only recognizes the encoding. If you copy a text file from a UTF-8 project to a GBK project and open it again, garbled characters will appear. However, if you directly copy the text content from the UTF-8 text file to the GBK project text file, there will be no garbled characters, and the system will automatically perform encoding conversion.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.