Use. Net to obtain the location code of Chinese Characters

Source: Internet
Author: User

First, review basic computer knowledge:

The underlying data in the computer is represented by binary and 0 and 1. Each 0 or 1 is called a 1-bit, and the 8th-bit binary number is called a 1-byte, which can represent a character in the ASCII code. A Chinese computer uses two bytes (16-bit binary) to represent a Chinese character. All symbols in Unicode encoding (including Chinese characters, English letters, titles, and many other symbols) are expressed in two-byte (16) bits.

In system. the text namespace contains many encoding classes for operation and conversion. The following two instances are used to swap the location codes and Chinese characters, hoping to achieve the opposite effect, this allows you to easily handle text encoding problems:

Using system;
Using system. text;
Class codingchange
{
Public String charactertocoding (string character)
{
String coding = "";
For (INT I = 0; I <character. length; I ++)
{
Byte [] bytes = system. Text. encoding. Unicode. getbytes (character. substring (I, 1); // retrieves binary encoding content
String lowcode = system. Convert. tostring (Bytes [0], 16); // extract the low-byte encoding content (two hexadecimal values)
If (lowcode. Length = 1)
Lowcode = "0" + lowcode;
String hightcode = system. Convert. tostring (Bytes [1], 16); // retrieves the High-byte encoding content (two hexadecimal values)
If (hightcode. Length = 1)
Hightcode = "0" + hightcode;
Coding + = (lowcode + hightcode); // Add it to the string,
}
Return coding;
}
Public String codingtocharacter (string coding)
{
String characters = "";
If (coding. Length % 4! = 0) // it must be a multiple of 4 in hexadecimal notation.
{
Throw new system. Exception ("Incorrect encoding format ");
}
For (INT I = 0; I <coding. length; I + = 4) // each four digits is a Chinese character
{
Byte [] bytes = new byte [2];
String lowcode = coding. substring (I, 2); // extracts the low byte and converts it in hexadecimal notation.
Bytes [0] = system. Convert. tobyte (lowcode, 16 );
String highcode = coding. substring (I + 2, 2); // extracts the high byte and converts it in hexadecimal notation.
Bytes [1] = system. Convert. tobyte (highcode, 16 );
String character = system. Text. encoding. Unicode. getstring (bytes );
Characters + = character;
}
Return characters;
}
Public static void main ()
{
Codingchange code = new codingchange ();
String coding = code. charactertocoding ("our big China is a good home .");
Console. writeline (coding );
Console. writeline (code. codingtocharacter (coding ));
}
};
The output result is as follows:

C: \> Test
1162ec4e847627592d4efd560cff2f667d5928578476004e2a4eb65b2e00
Our big China is a good home.

C: \>

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.