CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2003
    Posts
    20

    Extracting Chinese Character from a CString

    Dear all


    CString aStr="Learning ¤¤¤å"; //a mixed English and Chinese string

    Extracting each Chinese Character from The String and assign it to a UINT data type.

    UINT aWord = ????

    i use _MBCS not unicode

    can someone tell me how?

  2. #2
    Join Date
    Jun 1999
    Location
    Hong Kong
    Posts
    181
    For Big5 Chinese Character (2-byte char), the first byte is within a range (about 0xA1 to 0xFE). If it is in the range, then you have to extract both the current and next byte. Therefore, you combine the 2 extracted bytes to form back a Chinese Character.
    In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".

  3. #3
    Join Date
    Aug 2003
    Posts
    20
    Thanks for your reply.

    for extracting currenct byte, do you mean to use the IsDBCSLeadByte()?

    I really donot how to extract both the current and next byte, and combine the 2 extracted bytes to form back a Chinese Character?
    Last edited by wow9999; September 17th, 2003 at 05:14 AM.

  4. #4
    Join Date
    Jun 1999
    Location
    Hong Kong
    Posts
    181
    I hope the following code can help.
    Code:
    CString aStr = "Learning ¤¤¤å";
    char cChinese[3];
    cChinese[2] = '\0';
    int i = 0;
    while (i<aStr.GetLength()) {
    	if ((byte)aStr[i] >= 0xA1 && (byte)aStr[i] <=0xFE) {
    		cChinese[0] = aStr[i];
    		cChinese[1] = aStr[i+1];
    		AfxMessageBox(cChinese);
    		++i;
    	}
    	++i;
    }
    In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".

  5. #5
    Join Date
    Aug 2003
    Posts
    20
    Thanks

    code:--------------------------------------------------------------------------------
    CString aStr = "Learning ¤¤¤å";
    char cChinese[3];
    cChinese[2] = '\0';
    int i = 0;
    while (i<aStr.GetLength()) {
    if ((byte)aStr[i] >= 0xA1 && (byte)aStr[i] <=0xFE) {
    cChinese[0] = aStr[i];
    cChinese[1] = aStr[i+1];
    AfxMessageBox(cChinese);

    UINT aWord = cChinese // How to assign a Char to UINT

    ++i;
    }
    ++i;
    }
    --------------------------------------------------------------------------------
    Last edited by wow9999; September 17th, 2003 at 08:33 PM.

  6. #6
    Join Date
    Jun 1999
    Location
    Hong Kong
    Posts
    181
    Actually a UINT is a 4-byte variable while a Chinese Character is 2-byte. So how do you want to assign to the UINT?
    In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".

  7. #7
    Join Date
    Aug 2003
    Posts
    20
    HI

    DWORD GetGlyphOutline(
    HDC hdc, // handle to device context
    UINT uChar, // character to query
    UINT uFormat, // format of data to return
    LPGLYPHMETRICS lpgm, // pointer to structure for metrics
    DWORD cbBuffer, // size of buffer for data
    LPVOID lpvBuffer, // pointer to buffer for data
    CONST MAT2 *lpmat2 // pointer to transformation matrix structure
    );

    I use GetGlyphOutline function to retrieve the outline for a character in the True type font, so it need to use "UINT".

    It works ok when I assign a single Chinese Character to UINT.

    UINT aWord ='¤¤'; // works ok

    But for my program, the user will input a String; therefore, i have to extracte each character from the String and run GetGlyphOutline function for each character to get its outline.

  8. #8
    Join Date
    Jun 1999
    Location
    Hong Kong
    Posts
    181
    Code:
    CString aStr = "Learning ¤¤¤å";
    int i = 0;
    while (i<aStr.GetLength()) {
    	if ((byte)aStr[i] >= 0xA1 && (byte)aStr[i] <=0xFE) {
    		UINT aWord = 0;
    		((char*)&aWord)[0] = aStr[i+1];
    		((char*)&aWord)[1] = aStr[i];
    		++i;
    	}
    	++i;
    }
    In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".

  9. #9
    Join Date
    Aug 2003
    Posts
    20
    I am really thank you for help me to solve this problem. and i found another way to assign a Chinese Character to UINT


    Code:
    CString aStr = "Learning ¤¤¤å";
    int i = 0;
    while (i<aStr.GetLength()) {
    	if ((byte)aStr[i] >= 0xA1 && (byte)aStr[i] <=0xFE) {
                             int k1=(byte)aStr [i];
    	         int k2=(byte)aStr [i+1];
    	         UINT aWord=(k1*256)+k2; //(HiByte * 256 + LoByte)		
                         ++i;
    	}
    	++i;
    }
    What do you think about this method?

  10. #10
    Join Date
    Jun 1999
    Location
    Hong Kong
    Posts
    181
    Either yours and mine can solve the problem. :-)
    In my opinion, yours is better.
    In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured