As we know, in Windows, both Chinese and full-width characters account for two bytes and use the ASCII chart 2 (codes 128–255). We can, by this point, detect whether the user entered Chinese and full-width characters. In fact, the first byte of the full-width character is always set to 163, while the second byte is the same half-width character code plus 128 (excluding spaces). If the half angle A is 65, then full-width A is 163 (first byte), 193 (second byte, 128+65). And for Chinese, its first byte is set to be greater than 163, (such as ' A ' is: 176 162), we can detect Chinese without conversion.
The above is only for my personal test results, and I am learning to program less than half a month. If there are errors or leaks, please do not hesitate to enlighten us. Let me give you a routine for your reference.
Create a new form and place Button1, Edit1, and edit2 on the form.
/* Enter full-width characters, Chinese, half-width characters, or promiscuous input in the edit1. Click the text in Button1,edit1 to display in Edit2 and convert all full-width characters. Note that the program does not respond correctly to the special character Furu 255 (press ALT and press the keypad 2, 5, 5 input). */
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)//判断是否为全角字符
{
str=str+ansistring((char)(c2-128));
i++;
continue;
}
if (c1>163)//判断是否为文字
{
str=str+ansistring((char)c1);
str=str+ansistring((char)c2);
i++;
continue;
}
if ((c1==161) && (c2==161))//全角空格是个特例,另加处理
{
str=str+" ";
i++;
continue;
}
str=str+ ansistring(char(c1));
}
edit2->text=str;
delete ctmp;ctmp=null;
}
The above code is compiled by C++builder5.0/win98 SE.