Would anyone happen to know how to convert type LPTSTR
to char *
in C++?
Depends if it is Unicode or not it appears. LPTSTR is char* if not Unicode, or w_char* if so.
Discussed better here (accepted answer worth reading)
Here are a lot of ways to do this. MFC or ATL's CString, ATL macros, or Win32 API.
LPTSTR szString = _T("Testing");
char* pBuffer;
You can use ATL macros to convert:
USES_CONVERSION;
pBuffer = T2A(szString);
CString:
CStringA cstrText(szString);
or the Win32 API WideCharToMultiByte
if UNICODE
is defined.
If your compiler Character Setting is set to Unicode Character Set, then LPTSTR will be interpreted as wchar_t*. In that case Unicode to Multibyte character conversion is required.
(In Visual Studio, setting is located at Project Properties\Configuration Properties\General\Character Set)
The sample code below should give an idea:
#include <windows.h>
/* 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;
}
On the other hand, if your compiler Character Setting is set to Multibyte, then LPTSTR will be interpreted as char*.
In that case:
LPTSTR x = "test";
char* y;
y = x;
Also see:
Another discussion about wchar_t conversion: How do you properly use WideCharToMultiByte
MSDN Article: http://msdn.microsoft.com/en-us/library/dd374130(v=vs.85).aspx
Valid Code Page Identifiers: http://msdn.microsoft.com/en-us/library/dd317756(v=vs.85).aspx
char * pCopy = NULL;
if (sizeof(TCHAR) == sizeof(char))
{
size_t size = strlen(pOriginal);
pCopy = new char[size + 1];
strcpy(pCopy, pOriginal);
}
else
{
size_t size = wcstombs(NULL, pOriginal, 0);
pCopy = new char[size + 1];
wcstombs(pCopy, pOriginal, size + 1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With