C ++ Builder: How to convert full angle characters to half-width character?

zhaozj2021-02-17  54

We know that in Windows, Chinese and full-width characters take two bytes and use ASCII Chart 2 (CODES 128-255). We can use this to detect whether the user entered is Chinese and full-horn characters. In fact, the first byte of the full-horn characters is always set to 163, and the second byte is the same half-way character code plus 128 (excluding spaces). If the half angle A is 65, the full angle A is 163 (first byte), 193 (second byte, 128 65). For Chinese, its first byte is set to be greater than 163, (such as 'A' is: 176 162), we can do not convert in Chinese. The above is only for my personal trial, and I am more learning and programming is less than half a month. If there is a mistake or chapter, please let me know. Below I give a routine for your reference.

New form, place button1, edit1, and edit2 on the form.

/ * Enter a full-corner character, Chinese, half-width character, or mixed input in Edit1. Click Button1, the text in Edit1 is displayed in Edit 2 and converted all full-corner characters. Note that the program does not respond correctly to special characters such as 255 (holding down the Alt and 2, 5, 5 inputs of 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

Str = STR ANSISTRING ((C2-128)); i ; Continue;} if (c1> 163) // Judgment is a text {

Str = STR ANSISTRING ((char) C1); Str = Str Ansistring ((char) C2); i ; Continue;} IF ((C1 == 161) && (C2 == 161)) // Full-angle space is Special case, additional processing {STR = Str ""; i ; Continue;} Str = Str Ansistring (CHAR (C1));} edit2-> text = str; delete ctmp; ctmp = null;} The above code C Builder5 .0 / win98 seck under compilation.

9cbs_light (Scarecrow)

转载请注明原文地址:https://www.9cbs.com/read-30208.html

New Post(0)