Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert lptstr to char*

Tags:

c++

char

lptstr

Would anyone happen to know how to convert type LPTSTR to char * in C++?

like image 975
user37875 Avatar asked Dec 05 '08 02:12

user37875


4 Answers

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)

like image 160
JamesSugrue Avatar answered Oct 23 '22 06:10

JamesSugrue


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.

like image 21
John Z Avatar answered Oct 23 '22 07:10

John Z


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

like image 7
vahapt Avatar answered Oct 23 '22 06:10

vahapt


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);
}
like image 3
Mark Ransom Avatar answered Oct 23 '22 06:10

Mark Ransom