Java program implements Unicode code and Chinese to convert each other

Source: Internet
Author: User

According to the supplementary question in the previous article http://blog.csdn.net/fancylovejava/article/details/10142391

With an understanding of the previous article, I probably know the Unicode encoding format.

ANSI: The inner code range of the Chinese character area is high byte from B0-f7, low byte from A1-fe
Unicode: The Unicode encoding range for Chinese characters is \u4e00-\u9fa5 \uf900-\ufa2d, which is not a Chinese character if it is not within this range.

The problem with the program now is that when the server sends the Chinese to the Android client, the Android client gets the string and displays it to the interface, but the result is

Unicode encoding Format & #24320;& #22987;& #20817;& #22870; So, it's going to change.

There are related website tools for converting Unicode encoding and ASICC encoding, http://tool.chinaz.com/Tools/Unicode.aspx

JDK has a tool also provides conversion, is Native2ascii.exe, under the Bin directory, directly open input Chinese can be

There is a better article about http://sailinglee.iteye.com/blog/430568

But we will be in the process of these & #24320, converted into Chinese AH ~ ~ ~

String a= "Start redeeming";
System.out.println (A.codepointat (0));

This printed out is the "open" character of the & #24320; Unicode number part 24320

System.out.println ((char) 24320);

This printout is to convert a number to a char type, which is a Chinese character representing the Unicode code.

Print out the result: open

With this, you can convert Unicode into Chinese.

There is an article very good, turn around http://blog.csdn.net/ocean20/article/details/6743385 description under Char this character type in Java

Char in Java takes up a few bytes

1: "Byte" is byte, "bit" is bit;

2:1 byte = 8 bit;

Char is 2 bytes in Java. Java uses unicode,2 bytes (16 bits) to represent one character.

The example code is as follows:

[Java]View Plaincopy
  1. Public class Test {
  2. public static void Main (string[] args) {
  3. String str= "Medium";
  4. char x =' Medium ';
  5. byte[] bytes=null;
  6. byte[] bytes1=null;
  7. try {
  8. bytes = Str.getbytes ("Utf-8");
  9. Bytes1 = Chartobyte (x);
  10. } catch (Unsupportedencodingexception e) {
  11. //TODO auto-generated catch block
  12. E.printstacktrace ();
  13. }
  14. System.out.println ("bytes Size:" +bytes.length);
  15. System.out.println ("bytes1 size:" +bytes1.length);
  16. }
  17. public static byte[] Chartobyte (char c) {
  18. byte[] B = new byte[2];
  19. b[0] = (byte) ((C & 0xff00) >> 8);
  20. b[1] = (byte) (C & 0xFF);
  21. return B;
  22. }
  23. }

Operation Result:

Bytes Size: 3
Bytes1 Size: 2

  

Java is a Unicode representation of a character, and the Unicode of the Chinese character "medium" is 2 bytes.

The String.getbytes (encoding) method is to get a byte array representation of the specified encoding,

typically gbk/gb2312 is 2 bytes, and Utf-8 is 3 bytes .

If you do not specify encoding, the system default encoding is taken.

Java program implements Unicode code and Chinese to convert each other

Related Article

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.