none
The problem about CString & 0xFFFF RRS feed

  • Question

  • I met a issue when i use CString to store some specified bytes. i created a new 'Win32 Console Application' project with VS2010, checked the MFC. My question and source 

                CString str;
                TCHAR* p = new TCHAR[31];
                p[0] = 0xFFFF; //it will not the issue later if i change p[0] = 0xFFFB.
                p[1] = _T('A');
                p[2] = _T('A');
                p[3] = 0;
     
                CString strP = CString(p); //Result:strP == “AA”  
     
                str.Format(_T("%s--%s"), _T("ab"), strP); //Result:str == “”, Why? It's not what i expected.  
     
                str = _T("ab--") + strP; //Result:str == “ab--AA”,It is what i expected.
                 
                delete[] p;


    Wednesday, August 6, 2014 9:50 AM

Answers

  • "store some specified bytes"

    Attempting to store noncharacters in a string is asking for trouble. It shouldn't be a surprise that you get unexpected results.

    What happens in this particular case is that CString::Format uses vswprintf which in turn uses putwc. putwc is supposed to return the stored character or WEOF to indicate an error. The value of WEOF happens to be 0xffff so the vswprintf thinks that an error has occurred.

    • Marked as answer by woodyhfl Tuesday, August 12, 2014 7:39 AM
    Thursday, August 7, 2014 10:07 AM
    Moderator
  • Can you please explain the sense of the the 0xFFFF in p[0]?

    You problem is you are using in your string stuff the unicode char 0xFFFF. That causes your problem. I modified your code to show the problem:

                CString str;
                TCHAR* p = new TCHAR[31];
                p[0] = 0xFFFF; //it will not the issue later if i change p[0] = 0xFFFB.
                p[1] = _T('A');
                p[2] = _T('A');
                p[3] = 0;
     
                CString strP = CString(p); //Result:strP == “AA”  
                TCHAR* c=strP.GetBuffer();
     
                str.Format(_T("%s--%s"), _T("ab"), strP); //Result:str == “”, Why? It's not what i expected.  
                TCHAR* d=str.GetBuffer();
     
                str = _T("ab--") + strP; //Result:str == “ab--AA”,It is what i expected.
                 
                delete[] p;

    hen I display the result of the added line

    When I display the reswult of the added lines, I see the 0xFFFF in the CString buffer named strP also. For me it seems the problem is the 0xFFFF unicode char in CString::Format.

    At all unicode character 0xFFFF is a so called "noncharacter". This char codes are for unicode internal usage. It is not allowed to use this chars in text / strings. Therefore the code piece works 100% in an expect way since you are using a non allowed char code.

    Here the Unicode reference:

    http://en.wikibooks.org/wiki/Unicode/Character_reference/F000-FFFF

    About noncharacters:

    http://www.unicode.org/faq/private_use.html#nonchar1


    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.




    • Edited by Bordon Thursday, August 7, 2014 12:40 PM typo
    • Marked as answer by woodyhfl Tuesday, August 12, 2014 7:39 AM
    Thursday, August 7, 2014 10:00 AM

All replies

  • Hi woodyhfl,

    I am moving your thread into the Visual C++ Forum for dedicated support. Thanks for your understanding.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Thursday, August 7, 2014 7:34 AM
  • Can you please explain the sense of the the 0xFFFF in p[0]?

    You problem is you are using in your string stuff the unicode char 0xFFFF. That causes your problem. I modified your code to show the problem:

                CString str;
                TCHAR* p = new TCHAR[31];
                p[0] = 0xFFFF; //it will not the issue later if i change p[0] = 0xFFFB.
                p[1] = _T('A');
                p[2] = _T('A');
                p[3] = 0;
     
                CString strP = CString(p); //Result:strP == “AA”  
                TCHAR* c=strP.GetBuffer();
     
                str.Format(_T("%s--%s"), _T("ab"), strP); //Result:str == “”, Why? It's not what i expected.  
                TCHAR* d=str.GetBuffer();
     
                str = _T("ab--") + strP; //Result:str == “ab--AA”,It is what i expected.
                 
                delete[] p;

    hen I display the result of the added line

    When I display the reswult of the added lines, I see the 0xFFFF in the CString buffer named strP also. For me it seems the problem is the 0xFFFF unicode char in CString::Format.

    At all unicode character 0xFFFF is a so called "noncharacter". This char codes are for unicode internal usage. It is not allowed to use this chars in text / strings. Therefore the code piece works 100% in an expect way since you are using a non allowed char code.

    Here the Unicode reference:

    http://en.wikibooks.org/wiki/Unicode/Character_reference/F000-FFFF

    About noncharacters:

    http://www.unicode.org/faq/private_use.html#nonchar1


    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.




    • Edited by Bordon Thursday, August 7, 2014 12:40 PM typo
    • Marked as answer by woodyhfl Tuesday, August 12, 2014 7:39 AM
    Thursday, August 7, 2014 10:00 AM
  • "store some specified bytes"

    Attempting to store noncharacters in a string is asking for trouble. It shouldn't be a surprise that you get unexpected results.

    What happens in this particular case is that CString::Format uses vswprintf which in turn uses putwc. putwc is supposed to return the stored character or WEOF to indicate an error. The value of WEOF happens to be 0xffff so the vswprintf thinks that an error has occurred.

    • Marked as answer by woodyhfl Tuesday, August 12, 2014 7:39 AM
    Thursday, August 7, 2014 10:07 AM
    Moderator
  • Thanks.
    Tuesday, August 12, 2014 7:40 AM
  • Thanks for your detail answer.

    It's a long story about we used the 0xFFFF in string in our system. Anyway at present i think it is not a good decision as it leads to a potential errors when use some string functions.

    Tuesday, August 12, 2014 7:49 AM
  • Thanks.
    Tuesday, August 12, 2014 7:49 AM
  • That note is completely unrelated to this issue, it's about something like this:

    sprint(buffer, "%s", buffer);

    Tuesday, August 19, 2014 7:18 AM
    Moderator