Problem with converting std::string to CString
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Problem with converting std::string to CString

  1. #1
    Join Date
    Mar 2010
    Posts
    42

    Problem with converting std::string to CString

    Hi all,

    I am experiencing a problem in converting std::string to CString, and I don't know why it occurs. I am not using MFC. I just included atlstr.h.

    Here is the code I use:

    string mpname = "SCCMDC.SCCM.COM";

    CString sMPIP(mpname.c_str());

    cout << "sMPIP: " << sMPIP << endl;

    printf("Printf: &#37;s", sMPIP);
    printf("Printf2: %s", &sMPIP);

    cout returns something like this:

    sMPIP: 000000000003046F8

    While the two printfs returns something like this:

    Printf: <
    Printf: H=1

    I am not sure if the above is causing another error: adding two CStrings.

    CString sNewloc = "something" + cString1 + " something " + cString2;

    printf("new loc: %s", sNewloc);



    Am I converting it wrong or am I just printing it wrong? Can anyone help me with this?

    Thank you!
    Last edited by LeanA; May 14th, 2010 at 04:36 AM. Reason: Additional problem

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564

    Re: Problem with converting std::string to CString

    I guess that cout is converting the CString to void *. Try casting
    it to something that cout understand:

    Code:
    cout << "sMPIP: " << static_cast<LPCTSTR>(sMPIP) << endl;

  3. #3
    Join Date
    Nov 2003
    Posts
    1,793

    Re: Problem with converting std::string to CString

    I believe you meant LPCSTR, or ...
    Code:
    cout << "sMPIP: " << static_cast<const char*>(sMPIP) << endl;
    Since cout is char based.

    Do the same for printf("%s") parameters.

    Here's how to use code-tags: http://www.codeguru.com/forum/misc.php?do=bbcode#code

    gg

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Problem with converting std::string to CString

    I'm surprised cout isn't working. But yes, printf() expects a char* for a &#37;s. While CString contains an implicit conversion to char*, it will not work in a variadic argument list like printf has.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Problem with converting std::string to CString

    Quote Originally Posted by Lindley View Post
    I'm surprised cout isn't working.
    For wide strings, the stream to use is std::wcout. If you pass a wide string to cout, you get the addresses instead of the strings displayed.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Mar 2010
    Posts
    42

    Re: Problem with converting std::string to CString

    Hi all, thank you all for replying.

    I forgot to mention that I also tried wcout. It has the same output as in using cout.

    Or does wcout not work with CStrings too?

    Thank you!

  7. #7
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Problem with converting std::string to CString

    Quote Originally Posted by LeanA View Post
    Hi all, thank you all for replying.

    I forgot to mention that I also tried wcout. It has the same output as in using cout.

    Or does wcout not work with CStrings too?

    Thank you!
    There are two "versions" of CString, and it depends on the build that you are producing. If it's UNICODE build, then CString will contain a wide character string, else it will contain a string that is single-byte.

    That is the first issue -- sort out what build you are producing, Unicode or not Unicode. If it's Unicode, then the stream must be std::wcout when printing wide character strings, else the stream to use is std::cout.

    The second issue is that operator << knows nothing about CString -- that's why you get the hex output even if you are using std::wcout and you have a Unicode string.

    To tell operator << how to handle CString, the CString class has a casting operator (LPCTSTR) that returns a const char* (or const w_chart*) to operator <<, which that operator is overloaded to handle.

    So you have to make two things happen -- you use the correct stream (cout or wcout), depending on the build type (Unicode, non-Unicode) and you use the LPCTSTR cast operator for CString.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Nov 2003
    Posts
    1,793

    Re: Problem with converting std::string to CString

    Code:
        CStringA a = "Hello";
        CStringW w = L"World";
    
        wcout << static_cast<const wchar_t*>(a) << endl; // always an error
        cout << static_cast<const char*>(w) << endl; // always an error
    
        wcout << static_cast<LPCTSTR>(a) << endl; // compile error, or Ok
        cout << static_cast<LPCTSTR>(w) << endl; // compile error, or operator<<(const void*) invoked
    If you cast the CString based on the stream type, you'll always catch this mistake at compile time.

    gg

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center