I'm currently working at MFC applications of Visual Studio. And I mainly use string to log and debug, which may contain Chinese characters. So I always use Unicode Character Set by default. And the output routine, whether it's to the output window or to a text file, usually revolves around C-style string, CString and ostringsteam. Although I read about unicode encoding and I know how the Unicode or _Unicode identifier works. I'm still quite confused of how C-style string and CString are stored, converted and output.
Initial Question
CString CStr = _T("你好"); // Hello. U+4F60 U+597D
char* LPSTR1 ="我是刘嘉森\n" ; //I am Jiasen Liu. U+6211 U+662F U+5218 U+5609 U+68EE U+000A
CStr += LPSTR1;
std::ostringstream os;
std::wostringstream wos;
os << CT2CA(CStr);
wos << LPCWSTR(CStr);
OutputDebugStringA(os.str().c_str());
OutputDebugStringW(wos.str().c_str());
The above code segments use two ways to output a string to output window. And both produces the same result:
你好我是刘嘉森 你好我是刘嘉森
As I use CT2CA in the first method, I wonder how ANSI character string can successfully represent a Unicode character string composed of CJK characters.
Extended Question
Google didn't help me much in understanding the behavior in the initial question, as well as the general idea of how each character is manipulated internally. So I decided to have a test with all possible cross combinations of string type conversions and type casting using cout/wcout for output, and hopefully get a deeper understanding and solve questions related to string conversions once and for all. The test was performed in a console application with common header files for MFC, since I wanted to use CString but with a simpler output method. The results did clear things up a bit, but also generated more questions.
The following two blocks are the code segment and the corresponding output in console window.
LPCWSTR myStr = _T("How are you?");
CString myCStr = myStr;
LPCWSTR myLPCWSTR0 = myStr;
LPSTR myLPSTR1 = CT2A(myStr);
LPSTR myLPSTR1_1 = CT2CA(myStr);
LPCSTR myLPCSTR1 = CT2CA(myStr);
LPCSTR myLPCSTR1_1 = CT2A(myStr);
LPWSTR myLPWSTR1 = CT2W(myStr);
LPCWSTR myLPCWSTR1 = CT2CW(myStr);
LPCWSTR myLPCWSTR1_1 = CT2W(myStr);
LPCWSTR myLPCWSTR2 = myCStr;
LPSTR myLPSTR3 = CT2A(myCStr);
LPSTR myLPSTR3_1 = CT2CA(myCStr);
LPCSTR myLPCSTR3 = CT2CA(myCStr);
LPCSTR myLPCSTR3_1 = CT2A(myCStr);
LPWSTR myLPWSTR3 = CT2W(myCStr);
LPCWSTR myLPCWSTR3 = CT2CW(myCStr);
LPCWSTR myLPCWSTR3_1 = CT2W(myCStr);
std::cout << endl << "*********Following is cout:********" << endl;
std::cout << "1. "<< "myStr: " << myStr << " wcslen: " << wcslen(myStr) << " sizeof: " << sizeof(myStr) << endl;
std::cout << "2. "<< "(LPCWSTR)myStr: " << (LPCWSTR)myStr << " wcslen: " << wcslen((LPCWSTR)myStr) << " sizeof: " << sizeof((LPCWSTR)myStr) << endl;
std::cout << "3. "<< "LPCWSTR = myStr: " << myLPCWSTR0 << " wcslen: " << wcslen(myLPCWSTR0) << " sizeof: " << sizeof(myLPCWSTR0) << endl;
std::cout << "4. "<< "(LPSTR)myStr: " << (LPSTR)myStr << " strlen: " << strlen((LPSTR)myStr) << " sizeof: " << sizeof((LPSTR)myStr) << endl;
std::cout << "5. "<< "(LPCSTR)myStr: " << (LPCSTR)myStr << " strlen: " << strlen((LPCSTR)myStr) << " sizeof: " << sizeof((LPCSTR)myStr) << endl;
std::cout << endl;
std::cout << "6. "<< "CT2CA(myStr): " << CT2CA(myStr) << " strlen: " << strlen(CT2CA(myStr)) << " sizeof: " << sizeof(CT2CA(myStr)) << endl;
std::cout << "7. "<< "CT2A(myStr): " << CT2A(myStr) << " strlen: " << strlen(CT2A(myStr)) << " sizeof: " << sizeof(CT2A(myStr)) << endl;
std::cout << "8. "<< "CT2W(myStr): " << CT2W(myStr) << " wcslen: " << wcslen(CT2W(myStr)) << " sizeof: " << sizeof(CT2W(myStr)) << endl;
std::cout << "9. "<< "CT2CW(myStr): " << CT2CW(myStr) << " wcslen: " << wcslen(CT2CW(myStr)) << " sizeof: " << sizeof(CT2CW(myStr)) << endl;
std::cout << endl;
std::cout << "10. "<< "(LPSTR)CT2A(myStr): " << (LPSTR)CT2A(myStr) << " strlen: " << strlen((LPSTR)CT2A(myStr)) << " sizeof: " << sizeof((LPSTR)CT2A(myStr)) << endl;
std::cout << "11. "<< "(LPSTR)CT2CA(myStr): " << (LPSTR)CT2CA(myStr) << " strlen: " << strlen((LPSTR)CT2CA(myStr)) << " sizeof: " << sizeof((LPSTR)CT2CA(myStr)) << endl;
std::cout << "12. "<< "(LPCSTR)CT2CA(myStr): " << (LPCSTR)CT2CA(myStr) << " strlen: " << strlen((LPCSTR)CT2CA(myStr)) << " sizeof: " << sizeof((LPCSTR)CT2CA(myStr)) << endl;
std::cout << "13. "<< "(LPCSTR)CT2A(myStr): " << (LPCSTR)CT2A(myStr) << " strlen: " << strlen((LPCSTR)CT2A(myStr)) << " sizeof: " << sizeof((LPCSTR)CT2A(myStr)) << endl;
std::cout << "14. "<< "(LPWSTR)CT2W(myStr): " << (LPWSTR)CT2W(myStr) << " wcslen: " << wcslen((LPWSTR)CT2W(myStr)) << " sizeof: " << sizeof((LPWSTR)CT2W(myStr)) << endl;
std::cout << "15. "<< "(LPCWSTR)CT2CW(myStr): " << (LPCWSTR)CT2CW(myStr) << " wcslen: " << wcslen((LPCWSTR)CT2CW(myStr)) << " sizeof: " << sizeof((LPCWSTR)CT2CW(myStr)) << endl;
std::cout << "16. "<< "(LPCWSTR)CT2W(myStr): " << (LPCWSTR)CT2W(myStr) << " wcslen: " << wcslen((LPCWSTR)CT2W(myStr)) << " sizeof: " << sizeof((LPCWSTR)CT2W(myStr)) << endl;
std::cout << endl;
std::cout << "17. "<< "LPSTR = CT2A(myStr): " << myLPSTR1 << " strlen: " << strlen(myLPSTR1) << " sizeof: " << sizeof(myLPSTR1) << endl;
std::cout << "18. "<< "LPSTR = CT2CA(myStr): " << myLPSTR1_1 << " strlen: " << strlen(myLPSTR1_1) << " sizeof: " << sizeof(myLPSTR1_1) << endl;
std::cout << "19. "<< "LPCSTR = CT2CA(myStr): " << myLPCSTR1 << " strlen: " << strlen(myLPCSTR1) << " sizeof: " << sizeof(myLPCSTR1) << endl;
std::cout << "20. "<< "LPCSTR = CT2A(myStr): " << myLPCSTR1_1 << " strlen: " << strlen(myLPCSTR1_1) << " sizeof: " << sizeof(myLPCSTR1_1) << endl;
std::cout << "21. "<< "LPWSTR = CT2W(myStr): " << myLPWSTR1 << " wcslen: " << wcslen(myLPWSTR1) << " sizeof: " << sizeof(myLPWSTR1) << endl;
std::cout << "22. "<< "LPCWSTR = CT2CW(myStr): " << myLPCWSTR1 << " wcslen: " << wcslen(myLPCWSTR1) << " sizeof: " << sizeof(myLPCWSTR1) << endl;
std::cout << "23. "<< "LPCWSTR = CTCW(myStr): " << myLPCWSTR1_1 << " wcslen: " << wcslen(myLPCWSTR1_1) << " sizeof: " << sizeof(myLPCWSTR1_1) << endl;
std::cout << endl;
std::cout << endl;
std::cout << "24. "<< "myCStr: " << myCStr << " wcslen: " << wcslen(myCStr) << " sizeof: " << sizeof(myCStr) << endl;
std::cout << "25. "<< "(LPCWSTR)myCStr: " << (LPCWSTR)myCStr << " wcslen: " << wcslen((LPCWSTR)myCStr) << " sizeof: " << sizeof((LPCWSTR)myCStr) << endl;
std::cout << "26. "<< "LPCWSTR = myCStr: " << myLPCWSTR2 << " wcslen: " << wcslen(myLPCWSTR1) << " sizeof: " << sizeof(myLPCWSTR1) << endl;
std::cout << endl;
std::cout << "27. "<< "CT2A(myCStr): " << CT2A(myCStr) << " strlen: " << strlen(CT2A(myCStr)) << " sizeof: " << sizeof(CT2A(myCStr)) << endl;
std::cout << "28. "<< "CT2CA(myCStr): " << CT2CA(myCStr) << " strlen: " << strlen(CT2CA(myCStr)) << " sizeof: " << sizeof(CT2CA(myCStr)) << endl;
std::cout << "29. "<< "CT2W(myCStr): " << CT2W(myCStr) << " strlen: " << wcslen(CT2W(myCStr)) << " sizeof: " << sizeof(CT2W(myCStr)) << endl;
std::cout << "30. "<< "CT2CW(myCStr): " << CT2CW(myCStr) << " strlen: " << wcslen(CT2CW(myCStr)) << " sizeof: " << sizeof(CT2CW(myCStr)) << endl;
std::cout << endl;
std::cout << "31. "<< "(LPSTR)CT2CA(myCStr): " << (LPSTR)CT2A(myCStr) << " strlen: " << strlen((LPSTR)CT2A(myCStr)) << " sizeof: " << sizeof((LPSTR)CT2A(myCStr)) << endl;
std::cout << "32. "<< "(LPSTR)CT2CA(myCStr): " << (LPSTR)CT2A(myCStr) << " strlen: " << strlen((LPSTR)CT2A(myCStr)) << " sizeof: " << sizeof((LPSTR)CT2A(myCStr)) << endl;
std::cout << "33. "<< "(LPCSTR)CT2CA(myCStr): " << (LPCSTR)CT2CA(myCStr) << " strlen: " << strlen((LPCSTR)CT2CA(myCStr)) << " sizeof: " << sizeof((LPCSTR)CT2CA(myCStr)) << endl;
std::cout << "34. "<< "(LPCSTR)CT2CA(myCStr): " << (LPCSTR)CT2CA(myCStr) << " strlen: " << strlen((LPCSTR)CT2CA(myCStr)) << " sizeof: " << sizeof((LPCSTR)CT2CA(myCStr)) << endl;
std::cout << "35. "<< "(LPWSTR)CT2W(myCStr): " << (LPWSTR)CT2W(myCStr) << " wcslen: " << wcslen((LPWSTR)CT2W(myCStr)) << " sizeof: " << sizeof((LPWSTR)CT2W(myCStr)) << endl;
std::cout << "36. "<< "(LPCWSTR)CT2CW(myCStr): " << (LPCWSTR)CT2CW(myCStr) << " wcslen: " << wcslen((LPCWSTR)CT2CW(myCStr)) << " sizeof: " << sizeof((LPCWSTR)CT2CW(myCStr)) << endl;
std::cout << "37. "<< "(LPCWSTR)CT2W(myCStr): " << (LPCWSTR)CT2W(myCStr) << " wcslen: " << wcslen((LPCWSTR)CT2W(myCStr)) << " sizeof: " << sizeof((LPCWSTR)CT2W(myCStr)) << endl;
std::cout << "38. "<< "LPSTR = CT2A(myCStr): " << myLPSTR3 << " strlen: " << strlen(myLPCSTR3) << " sizeof: " << sizeof(myLPCSTR3) << endl;
std::cout << "39. "<< "LPSTR = CT2CA(myCStr): " << myLPSTR3_1 << " strlen: " << strlen(myLPCSTR3_1) << " sizeof: " << sizeof(myLPCSTR3_1) << endl;
std::cout << "40. "<< "LPCSTR = CT2CA(myCStr): " << myLPCSTR3 << " strlen: " << strlen(myLPCSTR3) << " sizeof: " << sizeof(myLPCSTR3) << endl;
std::cout << "41. "<< "LPCSTR = CT2A(myCStr): " << myLPCSTR3_1 << " strlen: " << strlen(myLPCSTR3_1) << " sizeof: " << sizeof(myLPCSTR3_1) << endl;
std::cout << "42. "<< "LPWSTR = CT2W(myCStr): " << myLPWSTR3 << " wcslen: " << wcslen(myLPCWSTR3) << " sizeof: " << sizeof(myLPCWSTR3) << endl;
std::cout << "43. "<< "LPCWSTR = CT2CW(myCStr): " << myLPCWSTR3 << " wcslen: " << wcslen(myLPCWSTR3) << " sizeof: " << sizeof(myLPCWSTR3) << endl;
std::cout << "44. "<< "LPCWSTR = CT2W(myCStr): " << myLPCWSTR3_1 << " wcslen: " << wcslen(myLPCWSTR3_1) << " sizeof: " << sizeof(myLPCWSTR3_1) << endl;
std::wcout << endl << "*********Following is wcout:********" << endl;
std::wcout << "45. "<< "myStr: " << myStr << " wcslen: " << wcslen(myStr) << " sizeof: " << sizeof(myStr) << endl;
std::wcout << "46. "<< "(LPCWSTR)myStr: " << (LPCWSTR)myStr << " wcslen: " << wcslen((LPCWSTR)myStr) << " sizeof: " << sizeof((LPCWSTR)myStr) << endl;
std::wcout << "47. "<< "LPCWSTR = myStr: " << myLPCWSTR0 << " wcslen: " << wcslen(myLPCWSTR0) << " sizeof: " << sizeof(myLPCWSTR0) << endl;
std::wcout << "48. "<< "(LPSTR)myStr: " << (LPSTR)myStr << " strlen: " << strlen((LPSTR)myStr) << " sizeof: " << sizeof((LPSTR)myStr) << endl;
std::wcout << "49. "<< "(LPCSTR)myStr: " << (LPCSTR)myStr << " strlen: " << strlen((LPCSTR)myStr) << " sizeof: " << sizeof((LPCSTR)myStr) << endl;
std::wcout << endl;
std::wcout << "50. "<< "CT2CA(myStr): " << CT2CA(myStr) << " strlen: " << strlen(CT2CA(myStr)) << " sizeof: " << sizeof(CT2CA(myStr)) << endl;
std::wcout << "51. "<< "CT2A(myStr): " << CT2A(myStr) << " strlen: " << strlen(CT2A(myStr)) << " sizeof: " << sizeof(CT2A(myStr)) << endl;
std::wcout << "52. "<< "CT2W(myStr): " << CT2W(myStr) << " wcslen: " << wcslen(CT2W(myStr)) << " sizeof: " << sizeof(CT2W(myStr)) << endl;
std::wcout << "53. "<< "CT2CW(myStr): " << CT2CW(myStr) << " wcslen: " << wcslen(CT2CW(myStr)) << " sizeof: " << sizeof(CT2CW(myStr)) << endl;
std::wcout << endl;
std::wcout << "54. "<< "(LPSTR)CT2A(myStr): " << (LPSTR)CT2A(myStr) << " strlen: " << strlen((LPSTR)CT2A(myStr)) << " sizeof: " << sizeof((LPSTR)CT2A(myStr)) << endl;
std::wcout << "55. "<< "(LPSTR)CT2CA(myStr): " << (LPSTR)CT2CA(myStr) << " strlen: " << strlen((LPSTR)CT2CA(myStr)) << " sizeof: " << sizeof((LPSTR)CT2CA(myStr)) << endl;
std::wcout << "56. "<< "(LPCSTR)CT2CA(myStr): " << (LPCSTR)CT2CA(myStr) << " strlen: " << strlen((LPCSTR)CT2CA(myStr)) << " sizeof: " << sizeof((LPCSTR)CT2CA(myStr)) << endl;
std::wcout << "57. "<< "(LPCSTR)CT2A(myStr): " << (LPCSTR)CT2A(myStr) << " strlen: " << strlen((LPCSTR)CT2A(myStr)) << " sizeof: " << sizeof((LPCSTR)CT2A(myStr)) << endl;
std::wcout << "58. "<< "(LPWSTR)CT2W(myStr): " << (LPWSTR)CT2W(myStr) << " wcslen: " << wcslen((LPWSTR)CT2W(myStr)) << " sizeof: " << sizeof((LPWSTR)CT2W(myStr)) << endl;
std::wcout << "59. "<< "(LPCWSTR)CT2CW(myStr): " << (LPCWSTR)CT2CW(myStr) << " wcslen: " << wcslen((LPCWSTR)CT2CW(myStr)) << " sizeof: " << sizeof((LPCWSTR)CT2CW(myStr)) << endl;
std::wcout << "60. "<< "(LPCWSTR)CT2W(myStr): " << (LPCWSTR)CT2W(myStr) << " wcslen: " << wcslen((LPCWSTR)CT2W(myStr)) << " sizeof: " << sizeof((LPCWSTR)CT2W(myStr)) << endl;
std::wcout << endl;
std::wcout << "61. "<< "LPSTR = CT2A(myStr): " << myLPSTR1 << " strlen: " << strlen(myLPSTR1) << " sizeof: " << sizeof(myLPSTR1) << endl;
std::wcout << "62. "<< "LPSTR = CT2CA(myStr): " << myLPSTR1_1 << " strlen: " << strlen(myLPSTR1_1) << " sizeof: " << sizeof(myLPSTR1_1) << endl;
std::wcout << "63. "<< "LPCSTR = CT2CA(myStr): " << myLPCSTR1 << " strlen: " << strlen(myLPCSTR1) << " sizeof: " << sizeof(myLPCSTR1) << endl;
std::wcout << "64. "<< "LPCSTR = CT2A(myStr): " << myLPCSTR1_1 << " strlen: " << strlen(myLPCSTR1_1) << " sizeof: " << sizeof(myLPCSTR1_1) << endl;
/*std::wcout << "65. "<< "LPWSTR = CT2W(myStr): " << myLPWSTR1 << " wcslen: " << wcslen(myLPWSTR1) << " sizeof: " << sizeof(myLPWSTR1) << endl;*/
std::wcout << "66. "<< "LPCWSTR = CT2CW(myStr): " << myLPCWSTR1 << " wcslen: " << wcslen(myLPCWSTR1) << " sizeof: " << sizeof(myLPCWSTR1) << endl;
/*std::wcout << "67. "<< "LPCWSTR = CTCW(myStr): " << myLPCWSTR1_1 << " wcslen: " << wcslen(myLPCWSTR1_1) << " sizeof: " << sizeof(myLPCWSTR1_1) << endl;*/
std::wcout << endl;
std::wcout << endl;
std::wcout << "68. "<< "myCStr: " << myCStr << " wcslen: " << wcslen(myCStr) << " sizeof: " << sizeof(myCStr) << endl;
std::wcout << "69. "<< "(LPCWSTR)myCStr: " << (LPCWSTR)myCStr << " wcslen: " << wcslen((LPCWSTR)myCStr) << " sizeof: " << sizeof((LPCWSTR)myCStr) << endl;
std::wcout << "70. "<< "LPCWSTR = myCStr: " << myLPCWSTR2 << " wcslen: " << wcslen(myLPCWSTR1) << " sizeof: " << sizeof(myLPCWSTR1) << endl;
std::wcout << endl;
std::wcout << "71. "<< "CT2A(myCStr): " << CT2A(myCStr) << " strlen: " << strlen(CT2A(myCStr)) << " sizeof: " << sizeof(CT2A(myCStr)) << endl;
std::wcout << "72. "<< "CT2CA(myCStr): " << CT2CA(myCStr) << " strlen: " << strlen(CT2CA(myCStr)) << " sizeof: " << sizeof(CT2CA(myCStr)) << endl;
std::wcout << "73. "<< "CT2W(myCStr): " << CT2W(myCStr) << " strlen: " << wcslen(CT2W(myCStr)) << " sizeof: " << sizeof(CT2W(myCStr)) << endl;
std::wcout << "74. "<< "CT2CW(myCStr): " << CT2CW(myCStr) << " strlen: " << wcslen(CT2CW(myCStr)) << " sizeof: " << sizeof(CT2CW(myCStr)) << endl;
std::wcout << endl;
std::wcout << "75. "<< "(LPSTR)CT2CA(myCStr): " << (LPSTR)CT2A(myCStr) << " strlen: " << strlen((LPSTR)CT2A(myCStr)) << " sizeof: " << sizeof((LPSTR)CT2A(myCStr)) << endl;
std::wcout << "76. "<< "(LPSTR)CT2CA(myCStr): " << (LPSTR)CT2A(myCStr) << " strlen: " << strlen((LPSTR)CT2A(myCStr)) << " sizeof: " << sizeof((LPSTR)CT2A(myCStr)) << endl;
std::wcout << "77. "<< "(LPCSTR)CT2CA(myCStr): " << (LPCSTR)CT2CA(myCStr) << " strlen: " << strlen((LPCSTR)CT2CA(myCStr)) << " sizeof: " << sizeof((LPCSTR)CT2CA(myCStr)) << endl;
std::wcout << "78. "<< "(LPCSTR)CT2CA(myCStr): " << (LPCSTR)CT2CA(myCStr) << " strlen: " << strlen((LPCSTR)CT2CA(myCStr)) << " sizeof: " << sizeof((LPCSTR)CT2CA(myCStr)) << endl;
std::wcout << "79. "<< "(LPWSTR)CT2W(myCStr): " << (LPWSTR)CT2W(myCStr) << " wcslen: " << wcslen((LPWSTR)CT2W(myCStr)) << " sizeof: " << sizeof((LPWSTR)CT2W(myCStr)) << endl;
std::wcout << "80. "<< "(LPCWSTR)CT2CW(myCStr): " << (LPCWSTR)CT2CW(myCStr) << " wcslen: " << wcslen((LPCWSTR)CT2CW(myCStr)) << " sizeof: " << sizeof((LPCWSTR)CT2CW(myCStr)) << endl;
std::wcout << "81. "<< "(LPCWSTR)CT2W(myCStr): " << (LPCWSTR)CT2W(myCStr) << " wcslen: " << wcslen((LPCWSTR)CT2W(myCStr)) << " sizeof: " << sizeof((LPCWSTR)CT2W(myCStr)) << endl;
std::wcout << "82. "<< "LPSTR = CT2A(myCStr): " << myLPSTR3 << " strlen: " << strlen(myLPCSTR3) << " sizeof: " << sizeof(myLPCSTR3) << endl;
std::wcout << "83. "<< "LPSTR = CT2CA(myCStr): " << myLPSTR3_1 << " strlen: " << strlen(myLPCSTR3_1) << " sizeof: " << sizeof(myLPCSTR3_1) << endl;
std::wcout << "84. "<< "LPCSTR = CT2CA(myCStr): " << myLPCSTR3 << " strlen: " << strlen(myLPCSTR3) << " sizeof: " << sizeof(myLPCSTR3) << endl;
std::wcout << "85. "<< "LPCSTR = CT2A(myCStr): " << myLPCSTR3_1 << " strlen: " << strlen(myLPCSTR3_1) << " sizeof: " << sizeof(myLPCSTR3_1) << endl;
/*std::wcout << "86. "<< "LPWSTR = CT2W(myCStr): " << myLPWSTR3 << " wcslen: " << wcslen(myLPCWSTR3) << " sizeof: " << sizeof(myLPCWSTR3) << endl;*/
std::wcout << "87. "<< "LPCWSTR = CT2CW(myCStr): " << myLPCWSTR3 << " wcslen: " << wcslen(myLPCWSTR3) << " sizeof: " << sizeof(myLPCWSTR3) << endl;
/*std::wcout << "88. "<< "LPCWSTR = CT2W(myCStr): " << myLPCWSTR3_1 << " wcslen: " << wcslen(myLPCWSTR3_1) << " sizeof: " << sizeof(myLPCWSTR3_1) << endl;*/
*********Following is cout:********
1. myStr: 0030ADDC wcslen: 12 sizeof: 4
2. (LPCWSTR)myStr: 0030ADDC wcslen: 12 sizeof: 4
3. LPCWSTR = myStr: 0030ADDC wcslen: 12 sizeof: 4
4. (LPSTR)myStr: H strlen: 1 sizeof: 4
5. (LPCSTR)myStr: H strlen: 1 sizeof: 4
6. CT2CA(myStr): How are you? strlen: 12 sizeof: 132
7. CT2A(myStr): How are you? strlen: 12 sizeof: 132
8. CT2W(myStr): 008FED28 wcslen: 12 sizeof: 260
9. CT2CW(myStr): 0030ADDC wcslen: 12 sizeof: 4
10. (LPSTR)CT2A(myStr): How are you? strlen: 12 sizeof: 4
11. (LPSTR)CT2CA(myStr): How are you? strlen: 12 sizeof: 4
12. (LPCSTR)CT2CA(myStr): How are you? strlen: 12 sizeof: 4
13. (LPCSTR)CT2A(myStr): How are you? strlen: 12 sizeof: 4
14. (LPWSTR)CT2W(myStr): 008FE698 wcslen: 12 sizeof: 4
15. (LPCWSTR)CT2CW(myStr): 0030ADDC wcslen: 12 sizeof: 4
16. (LPCWSTR)CT2W(myStr): 008FE468 wcslen: 12 sizeof: 4
17. LPSTR = CT2A(myStr): 烫烫烫烫 strlen: 136 sizeof: 4
18. LPSTR = CT2CA(myStr): 烫烫烫烫€鴱 strlen: 139 sizeof: 4
19. LPCSTR = CT2CA(myStr): 烫烫烫烫赭? strlen: 139 sizeof: 4
20. LPCSTR = CT2A(myStr): 烫烫烫烫h鲝 strlen: 139 sizeof: 4
21. LPWSTR = CT2W(myStr): 008FF5D0 wcslen: 412 sizeof: 4
22. LPCWSTR = CT2CW(myStr): 0030ADDC wcslen: 12 sizeof: 4
23. LPCWSTR = CTCW(myStr): 008FF4B8 wcslen: 552 sizeof: 4
24. myCStr: 00A30888 wcslen: 12 sizeof: 4
25. (LPCWSTR)myCStr: 00A30888 wcslen: 12 sizeof: 4
26. LPCWSTR = myCStr: 00A30888 wcslen: 12 sizeof: 4
27. CT2A(myCStr): How are you? strlen: 12 sizeof: 132
28. CT2CA(myCStr): How are you? strlen: 12 sizeof: 132
29. CT2W(myCStr): 008FE020 strlen: 12 sizeof: 260
30. CT2CW(myCStr): 00A30888 strlen: 12 sizeof: 4
31. (LPSTR)CT2CA(myCStr): How are you? strlen: 12 sizeof: 4
32. (LPSTR)CT2CA(myCStr): How are you? strlen: 12 sizeof: 4
33. (LPCSTR)CT2CA(myCStr): How are you? strlen: 12 sizeof: 4
34. (LPCSTR)CT2CA(myCStr): How are you? strlen: 12 sizeof: 4
35. (LPWSTR)CT2W(myCStr): 008FD990 wcslen: 12 sizeof: 4
36. (LPCWSTR)CT2CW(myCStr): 00A30888 wcslen: 12 sizeof: 4
37. (LPCWSTR)CT2W(myCStr): 008FD760 wcslen: 12 sizeof: 4
38. LPSTR = CT2A(myCStr): 烫烫烫烫隔? strlen: 139 sizeof: 4
39. LPSTR = CT2CA(myCStr): 烫烫烫烫,魪 strlen: 139 sizeof: 4
40. LPCSTR = CT2CA(myCStr): 烫烫烫烫狊? strlen: 139 sizeof: 4
41. LPCSTR = CT2A(myCStr): 烫烫烫烫髲 strlen: 139 sizeof: 4
42. LPWSTR = CT2W(myCStr): 008FF17C wcslen: 12 sizeof: 4
43. LPCWSTR = CT2CW(myCStr): 00A30888 wcslen: 12 sizeof: 4
44. LPCWSTR = CT2W(myCStr): 008FF064 wcslen: 1106 sizeof: 4
*********Following is wcout:********
45. myStr: How are you? wcslen: 12 sizeof: 4
46. (LPCWSTR)myStr: How are you? wcslen: 12 sizeof: 4
47. LPCWSTR = myStr: How are you? wcslen: 12 sizeof: 4
48. (LPSTR)myStr: H strlen: 1 sizeof: 4
49. (LPCSTR)myStr: H strlen: 1 sizeof: 4
50. CT2CA(myStr): How are you? strlen: 12 sizeof: 132
51. CT2A(myStr): How are you? strlen: 12 sizeof: 132
52. CT2W(myStr): 008FD318 wcslen: 12 sizeof: 260
53. CT2CW(myStr): 0030ADDC wcslen: 12 sizeof: 4
54. (LPSTR)CT2A(myStr): How are you? strlen: 12 sizeof: 4
55. (LPSTR)CT2CA(myStr): How are you? strlen: 12 sizeof: 4
56. (LPCSTR)CT2CA(myStr): How are you? strlen: 12 sizeof: 4
57. (LPCSTR)CT2A(myStr): How are you? strlen: 12 sizeof: 4
58. (LPWSTR)CT2W(myStr): How are you? wcslen: 12 sizeof: 4
59. (LPCWSTR)CT2CW(myStr): How are you? wcslen: 12 sizeof: 4
60. (LPCWSTR)CT2W(myStr): How are you? wcslen: 12 sizeof: 4
61. LPSTR = CT2A(myStr): 烫烫烫烫 strlen: 136 sizeof: 4
62. LPSTR = CT2CA(myStr): 烫烫烫烫€鴱 strlen: 139 sizeof: 4
63. LPCSTR = CT2CA(myStr): 烫烫烫烫赭? strlen: 139 sizeof: 4
64. LPCSTR = CT2A(myStr): 烫烫烫烫h鲝 strlen: 139 sizeof: 4
66. LPCWSTR = CT2CW(myStr): How are you? wcslen: 12 sizeof: 4
68. myCStr: 00A30888 wcslen: 12 sizeof: 4
69. (LPCWSTR)myCStr: How are you? wcslen: 12 sizeof: 4
70. LPCWSTR = myCStr: How are you? wcslen: 12 sizeof: 4
71. CT2A(myCStr): How are you? strlen: 12 sizeof: 132
72. CT2CA(myCStr): How are you? strlen: 12 sizeof: 132
73. CT2W(myCStr): 008FC610 strlen: 12 sizeof: 260
74. CT2CW(myCStr): 00A30888 strlen: 12 sizeof: 4
75. (LPSTR)CT2CA(myCStr): How are you? strlen: 12 sizeof: 4
76. (LPSTR)CT2CA(myCStr): How are you? strlen: 12 sizeof: 4
77. (LPCSTR)CT2CA(myCStr): How are you? strlen: 12 sizeof: 4
78. (LPCSTR)CT2CA(myCStr): How are you? strlen: 12 sizeof: 4
79. (LPWSTR)CT2W(myCStr): How are you? wcslen: 12 sizeof: 4
80. (LPCWSTR)CT2CW(myCStr): How are you? wcslen: 12 sizeof: 4
81. (LPCWSTR)CT2W(myCStr): How are you? wcslen: 12 sizeof: 4
82. LPSTR = CT2A(myCStr): 烫烫烫烫隔? strlen: 139 sizeof: 4
83. LPSTR = CT2CA(myCStr): 烫烫烫烫,魪 strlen: 139 sizeof: 4
84. LPCSTR = CT2CA(myCStr): 烫烫烫烫狊? strlen: 139 sizeof: 4
85. LPCSTR = CT2A(myCStr): 烫烫烫烫髲 strlen: 139 sizeof: 4
87. LPCWSTR = CT2CW(myCStr): How are you? wcslen: 12 sizeof: 4
请按任意键继续. . .
note: "请按任意键继续. . ." is "Press any key to continue..." in Chinese.
I drew the following assumptions and questions from the results:
Assumptions:
- If cout doesn't recognize the type of C-style string, it outputs address(Case 1), otherwise, it tries to decode and output string characters(Case 6).
- If the destination type of string conversion macros is the same as the source type, it returns the original address(Case 9); otherwise it returns the address of newly allocated memory(Case 8).
- Based on assumptions 1 and 2, the garbled characters in Case 17 could be explained as: since cout recognizes LPSTR, it tries to output string characters, and CT2A returns address of newly allocated memory which already destroyed with the temp CT2A object, hence the result.
- In all cases where the output of "sizeof" is larger than 4, it represents the size of conversion macro class type.
Note: The assumptions maybe all wrong. I list them to reveal my current level of understanding and hope you guys can help me correct my misconceptions.
Questions:
- In case 27 28 31 32 33 34, how an ANSI character string successfully represent a wide character string? (which is the same question as the initial question)
- For "(LPSTR)myStr", the output from cout is "H", does cout treat the first byte of "o", 0x00, as a null character? Why doesn't it stop at the first byte of "H"?
- In wcout part, although "myStr" or "myCString" is converted to ANSI strings, why wcout is still able to output the correct string? Doesn't it only recognize wide character strings?
- In cases 52 53 68 73 74, based on my assumption 1, why wcout is not able to recognize wide character strings but just output the address?
I commented out case 65 67 86 88, because every time the program runs to these lines, it produces an empty output, and skips the rest of the lines. The last line of the output, if I uncomment the four cases, would be like this
65. LPWSTR = CT2W(myStr): 请按任意键继续. . .
I actually test with "myStr" assigned a Chinese character string
LPCWSTR myStr = _T("你好吗?");//How are you?
The output for "你好吗" shows similar results as the one for "How are you?", except case 45 46 47 58 59 60 65 66 67 69 70 79 81 86 87 88, shows behaviors described in Question 5. Why is that?
I know this post may contain lots of questions, but I think they're all related and maybe one or two underlying concept can solve all my questions.