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.