How do I convert full-width characters to half-width characters? ZZ is common for all programming ..

Source: Internet
Author: User
We know that in windows, both Chinese characters and full-byte characters occupy two bytes, and ASCII Chart 2 (codes 128? C255 ). We can use this to check whether the user inputs Chinese characters and full-byte characters. In fact, the first byte of the fullwidth character is always set to 163, while the second byte is the same half-width bytecode plus 128 (excluding spaces ). For example, if the half-width A is 65, the full-width A is 163 (the first byte) and 193 (the second byte, 128 + 65 ). For Chinese, its first byte is set to greater than 163 (for example, 'A' is: 176 162). We can not convert it when detecting Chinese characters.
The above is only my personal experience, and I have learned programming for less than half a month. If any errors or omissions occur, please do not hesitate to inform us. Below is a routine for your reference.

Create a new form and place button1, edit1, and edit2.

/* Enter full-width characters, Chinese characters, half-width characters, or mixed characters in edit1. Click button1. The text in edit1 is displayed in edit2 and all full-width characters are converted. Note that the program does not correctly respond to special characters such as 255 (press ALT and press 2, 5, and 5 on the keypad. */

Void _ fastcall tform1: button1click (tobject * sender)
{
Int nlength = edit1-> text. Length ();
If (nlength = 0)
Return;
Ansistring STR = "";
Char * CTMP = new char [nlength + 1];
Strpcopy (CTMP, edit1-> text );
Byte C1, C2;
Int I;
For (I = 0; I <nlength; I ++)
{
C1 = CTMP [I];
C2 = CTMP [I + 1];
If (C1 = 163) // determines whether it is a fullwidth character
{

STR = STR + ansistring (char) (c2-128 ));
I ++;
Continue;
}
If (C1> 163) // determines whether it is a text
{

STR = STR + ansistring (char) C1 );
STR = STR + ansistring (char) C2 );
I ++;
Continue;
}
If (C1 = 161) & (C2 = 161) // All-round spaces are special cases.
{
STR = STR + "";
I ++;
Continue;
}
STR = STR + ansistring (char (C1 ));
}
Edit2-> text = STR;
Delete CTMP; CTMP = NULL;
}

The above code is compiled in C ++ builder5.0/Win98 se.

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.