Winform Learning Log (27th) ---------- unicode encoding, character conversion, and obtained Chinese character location code

Source: Internet
Author: User

I. unicode encoding and character conversion

Ii. unicode encoding and character conversion code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace ASCII {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private void btn_ToASCII_Click (object sender, EventArgs e) {if (txt_char.Text! = String. empty) // determines whether the input is null {if (Encoding. getEncoding ("unicode "). // determine whether the input is a letter, number, punctuation, and other GetBytes (new char [] {txt_char.Text [0]}) [1] = 0) // The encoded 1st-bit value is 0 {txt_ASCII.Text = Encoding. getEncoding (// get the character's ASCII code value "unicode "). getBytes (txt_char.Text) [0]. toString (); MessageBox. show (txt_char.Text [0]. toString ();} else {txt_ASCII.Text = string. empty; // output the Empty string MessageBox. show ("enter a letter! "," Prompt! "); // Prompt User information} private void btn_ToChar_Click (object sender, EventArgs e) {if (txt_ASCII2.Text! = String. empty) // determines whether the input is null {int P_int_Num; // defines the integer local variable if (int. tryParse (// convert the input characters to numbers txt_ASCII2.Text, out P_int_Num) {txt_Char2.Text = (char) P_int_Num ). toString (); // convert ASCII code to character} else {MessageBox. show (// If the input does not meet the requirements, the prompt box "enter the correct ASCII code value. "," Error! ") ;}} String P_str_temp =" abc "; string P_str = Encoding. getEncoding ("unicode "). getBytes (new char [] {P_str_temp [0]}) [0]. toString ();}}}
3. Obtain the location code of Chinese Characters

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace ChineseCode {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private void btn_Get_Click (object sender, EventArgs e) {if (txt_Chinese.Text! = String. empty) // judge whether the input is null {try {txt_Num.Text = // obtain the Chinese character location code (getCode (txt_Chinese.Text);} catch (IndexOutOfRangeException ex) {MessageBox. show (// use the message dialog box to prompt for exception information ex. message + "enter correct Chinese characters", "error! ");}}} /// <Summary> /// method for obtaining the Chinese character location code /// </summary> /// <param name = "strChinese"> Chinese Character </param> /// <returns> return the Chinese character location code </returns> public string getCode (string Chinese) {// (the 'location code is the one-to-one encoding corresponding to Chinese characters. It is represented by four digits. // The first two are the ID codes from 01 to 94, and the last two are the ID codes from 01 to 94. The first half of a Chinese character is the character with the ASC Ⅱ code being "160 + code", and the second half is the character with the ASC Ⅱ code being "160 + bits. 'Example: the location code of "Liu" is 3385, // It indicates the location code 33, 85, it consists of two characters, ASC Ⅱ code 160 + 33 = 193 and 160 + 85 = 245. Byte [] P_bt_array = Encoding. default. getBytes (Chinese); // get the Byte array of Chinese characters int front = (short) (P_bt_array [0]-'\ 0 '); // convert the first byte array to the short type. Here (short) code [0] is also applicable to int back = (short) (P_bt_array [1]-'\ 0'); // converts the second byte array to return (front-160) of the short type ). toString () + (Return-160 ). toString (); // calculates and returns the location code }}}

 

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.