How to convert full-width characters to half-width characters

Source: Internet
Author: User

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.

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.