how can convert LPCSTR string into LPCTSTR string?

后端 未结 4 1457
迷失自我
迷失自我 2021-01-22 19:35

i am trying to convert a LPCSTR string into LPCTSTR string. i want to concatenate two string,when i try like this

LPCTSTR str1 = L\"Ra         


        
相关标签:
4条回答
  • 2021-01-22 19:55

    See these notes: Unraveling Strings in Visual C++. The notes discuss the various kinds of strings you'll see in Microsoft C++ development and how to convert between them.

    0 讨论(0)
  • 2021-01-22 20:05

    MultiByteToWideChar is the only way to go, if your code is compiled with UNICODE.

    Alternatively you can do this. 7bits ASCII -> wchar should be easy.

    TCHAR str3[256] = { 0 };
    for (int i = 0; str2[i] != 0; i++) str3[i] = str2[i];
    
    0 讨论(0)
  • 2021-01-22 20:12

    LPCTSTR can be either plain char or wide characters depending on your project settings. Further, you cannot possibly concatenate a wide string and a plain char string. You need to convert one to a compatible form (wide to multibyte or vice versa) and then concatenate.

    Assuming you want the target to be a wide string, you'd need to convert the "Kumar" to a wide character string. To do this use the MultiByteToWideChar function with appropriate code page.

    Look up this KB article on MSDN and John's link.

    0 讨论(0)
  • 2021-01-22 20:19

    I tried this it worked:

    #include <iostream>
    #include <atlstr.h>
    
    using namespace std;
    int main()
    {
        LPCSTR stringToConvert = "My troublesome string";
        CString transitionString = stringToConvert;
        LPCTSTR myDesiredString = transitionString;
    
        wcout  << myDesiredString;
    }
    
    0 讨论(0)
提交回复
热议问题