Home Java javaTutorial Character encoding for basic introduction to Java

Character encoding for basic introduction to Java

Jul 21, 2017 pm 02:40 PM
java Base coding

1. ASII

American (national) Information Interchange Standard (code) code.

There are only numbers in the computer. Everything is represented by numbers, and the characters displayed on the screen are no exception.

The number that can be represented by one byte is 0-255, which is enough to display all the characters on the keyboard. For example, a is 97 and b is 98. This encoding rule corresponding to numbers and characters is called Asc11 code. The highest bits of ASC11 code are all 0, that is to say, the values ​​of ASC11 code are between 0-127.

2. GB2312 and GBK (China’s local character set)

Mainland China uses 2 bytes to represent each Chinese character, and the Chinese character The highest bits of each section are all 1. This encoding format is called (gb2312) national standard code. Then the numbers corresponding to the gb2312 code are negative numbers.

On the basis of gb2312, some more are added, such as traditional Chinese characters, called GBK

Attachment:

GB18030 encoding is an expansion based on GBK encoding, because There are more Chinese characters, and only using two-digit encoding can no longer accommodate the required Chinese characters, so a 2\4-bit mixed method is adopted to support more Chinese character encodings.

3. ANSI

In order to expand ASCII coding to display the national language, different countries and regions have formulated different standards, resulting in GB2312 , BIG5, JIS and other respective coding standards. These various Chinese extended encoding methods that use 2 bytes to represent a character are called ANSI encoding, also known as "MBCS (Muilti-Bytes Charecter Set, multi-byte character set)". Under the Simplified Chinese system, ANSI encoding represents GB2312 encoding. Under the Japanese operating system, ANSI encoding represents JIS encoding. Therefore, to transcode to gb2312 under Chinese windows, gbk only needs to save the text as ANSI encoding. Different ANSI encodings are incompatible with each other.

4. Local character set

On the computer system used in mainland China, GBK and GB2312 are called the local character set of the system.

The Chinese character for "中国" is encoded in hexadecimal D6D0 in mainland China, and A4A4 in Taiwan. The encoding in Taiwan is called BIG5 Big Five code. When a character appears in the localization system of one country and is transmitted to the localization system of another country via email, what you see is not the original character, but the characters or garbled characters of another country.

5. Unicode encoding

The ISO organization has unified the symbols around the world and calls it Unicode encoding.

The symbol "中" is the hexadecimal 4e2d all over the world. If all computers use Unicode encoding, the word "中" will be displayed as "中" on computers all over the world. Unicode-encoded characters occupy two bytes, and the AC11 code represents The character simply adds a byte with all bits equal to 0 in front of the byte originally occupied by the AS11 code. The number of characters it represents will not exceed 65535. In fact, it still retains more than 2,000 Values ​​are not used for encoding.

Unicode has not yet formed a unified world. For a long time, localized character encoding will coexist with Unicode encoding.

The characters in Java all use Unicode encoding.

Java also supports local platform character sets on the premise of ensuring cross-platform features through Unicode.

6. UTF-8

In the development process of Java language and other programs, especially XML also involves UTF-8 UTF-16. Unicode in a broad sense also includes UTF8 and utf-16

UTF-8

--ASC11 code characters remain intact and only occupy one byte.

--For characters from other countries, UTF-8 uses 2 or three bytes to represent them.

--Using UTF-8 encoded files, usually use EF BB BF as the three bytes of data at the beginning of the file.

7. Conversion rules between UTF-8 and unicode encoding

-- 0001-007f (one byte)

0xxxxxx

-- 0000 or its characters ranging from 0080 to 07ff,

110xxxxx 10xxxxxx (11 valid bits) (between 0080-07ff) A unicode has 16 bits, actually There are only 11 valid bits, the rest are flags.

-- Characters between 0800 and ffff, 1110xxxx 10xxxxxx 10xxxxxx (16 significant bits), the software can easily determine the occupied space of a character based on the fixed bit values ​​in UTF-8 encoding One byte, two bytes, or three bytes.

8. Advantages of UTF-8

-- ox00 does not appear (in C language, \0 represents the end of the string, indicating that the character has been reached The end of the string) Unicode For ACS11 characters, it takes up two bytes, adding a byte with empty content (0x00), which is wasteful, and this byte has special applications in C language and other programs.

-- It is convenient for applications to check whether errors have occurred during data transmission. It can check whether errors have occurred during data transmission.

-- Directly process documents using ASC11

9. China Unicom, Lenovo and United

Enter Unicom Lenovo, United, View in Notepad .You will see some error situations respectively

Unicom (or contact) garbled characters



Open with ue, check the hexadecimal, they are C1AA CDA8 C1AA CFE8 //These are all using GB2312 encoding. If it is medium, it is D6D0, that is, C1AA is the link CDA8 is the same as CFE8. The binary representation of it can be obtained in the following way:

int x=0xCDA8;
System.out.println(Integer.toBinaryString(x)  );
Copy after login

//11000001 10101010 Link 11001101 10101000 Through the file in

Notepad, the default is the Chinese character set GB2312 is used to store Zhu, so the character "Lian" is parsed into 1100 0001 1010 1010, and the character "通" is stored as 1100 1101 1010 1000. When the Notepad document is opened, these binary forms happen to correspond. UTF-8 rules, so the system thinks this is a UTF-8 encoded file and interprets it according to UTF-8. Garbled characters appear. The solution: when saving, press UTF-8 directly to save and it will not appear. .

10. Use the program to check the character encoding

View the GB2312 code of Chinese characters

View the UTF-8 code of Chinese characters

View the Unicode code of Chinese characters

public static void main(String[] args) throws UnsupportedEncodingException {
          String str="中国";        //查看字符的unicode码,将一个字符转成整数,得到的就是unicode值/*    for(int i=0;i<str.length();i++){
            int unicodeCode=str.charAt(i);
            System.out.println(unicodeCode); // 20013,22269
            
            System.out.println(Integer.toHexString(unicodeCode)); //对应的16进制 4e2d,56fd
        }*///查看字符的gb2312码byte [] buff =str.getBytes("gb2312");        for(int i=0;i<buff.length;i++){
            System.out.println(buff[i]); // -42,-48,   -71,-6System.out.println(Integer.toHexString(buff[i]));  //ffffffd6 ,ffffffd0    ffffffb9,fffffffa        }
    }
Copy after login

The above is the detailed content of Character encoding for basic introduction to Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles