Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If your compiler Character Setting is set to <strong>Unicode Character Set</strong>, then <strong>LPTSTR</strong> will be interpreted as <strong>wchar_t*</strong>. In that case Unicode to Multibyte character conversion is required.<br> (In Visual Studio, setting is located at Project Properties\Configuration Properties\General\Character Set)<br></p> <p>The sample code below should give an idea:</p> <pre><code>#include &lt;windows.h&gt; /* string consisting of several Asian characters */ LPTSTR wcsString = L"\u9580\u961c\u9640\u963f\u963b\u9644"; //LPTSTR wcsString = L"OnlyAsciiCharacters"; char* encode(const wchar_t* wstr, unsigned int codePage) { int sizeNeeded = WideCharToMultiByte(codePage, 0, wstr, -1, NULL, 0, NULL, NULL); char* encodedStr = new char[sizeNeeded]; WideCharToMultiByte(codePage, 0, wstr, -1, encodedStr, sizeNeeded, NULL, NULL); return encodedStr; } wchar_t* decode(const char* encodedStr, unsigned int codePage) { int sizeNeeded = MultiByteToWideChar(codePage, 0, encodedStr, -1, NULL, 0); wchar_t* decodedStr = new wchar_t[sizeNeeded ]; MultiByteToWideChar(codePage, 0, encodedStr, -1, decodedStr, sizeNeeded ); return decodedStr; } int main(int argc, char* argv[]) { char* str = encode(wcsString, CP_UTF8); //UTF-8 encoding wchar_t* wstr = decode(str, CP_UTF8); //If the wcsString is UTF-8 encodable, then this comparison will result to true. //(As i remember some of the Chinese dialects cannot be UTF-8 encoded bool ok = memcmp(wstr, wcsString, sizeof(wchar_t) * wcslen(wcsString)) == 0; delete str; delete wstr; str = encode(wcsString, 20127); //US-ASCII (7-bit) encoding wstr = decode(str, 20127); //If there were non-ascii characters existing on wcsString, //we cannot return back, since some of the data is lost ok = memcmp(wstr, wcsString, sizeof(wchar_t) * wcslen(wcsString)) == 0; delete str; delete wstr; } </code></pre> <p>On the other hand, if your compiler Character Setting is set to Multibyte, then <strong>LPTSTR</strong> will be interpreted as <strong>char*</strong>.<br></p> <p>In that case:</p> <pre><code>LPTSTR x = "test"; char* y; y = x; </code></pre> <p>Also see:</p> <p>Another discussion about wchar_t conversion: <a href="https://stackoverflow.com/questions/215963/how-do-you-properly-use-widechartomultibyte">How do you properly use WideCharToMultiByte</a><br> MSDN Article: <a href="http://msdn.microsoft.com/en-us/library/dd374130(v=vs.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/dd374130(v=vs.85).aspx</a><br> Valid Code Page Identifiers: <a href="http://msdn.microsoft.com/en-us/library/dd317756(v=vs.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/dd317756(v=vs.85).aspx</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload