会有人碰巧知道如何转换的类型 LPTSTRchar * C++?

有帮助吗?

解决方案

取决于如果它是Unicode或不出现。LPTSTR是char*如果不是Unicode,或w_char*如果是这样。

讨论更好地在这里 (接受的答案,值得一读)

其他提示

这里有很多方法可以做到这一点。MFC或ATL的CString,ATL宏或Win32API。

LPTSTR szString = _T("Testing");
char* pBuffer;

你可以使用ATL宏转换:

USES_CONVERSION;
pBuffer = T2A(szString);

CString:

CStringA cstrText(szString);

或Win32API WideCharToMultiByte 如果 UNICODE 定义。

如果你的字符编译器设置 Unicode Character Set, 然后 LPTSTR 会被解释为 wchar_t*.在这种情况下Unicode字节的文字转换是必需的。
(Visual Studio中,设置位于项目的性\结构性\一般\字符集)

下面的示例代码应该给一个想法:

#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;
}

另一方面,如果你的字符编译器设置多,然后 LPTSTR 会被解释为 char*.

在这种情况下:

LPTSTR x = "test";
char* y;
y = x;

也参见:

另一个讨论有关wchar_t转换: 你怎么正确使用WideCharToMultiByte
MSDN文章: http://msdn.microsoft.com/en-us/library/dd374130(v=与85).aspx
有效的页码标识符: http://msdn.microsoft.com/en-us/library/dd317756(v=与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);
}

好的,所以可以说,你有使用Unicode。你使用的某些功能,如LookupAccountSid,这需要对节目的功能,但他们返回LPTSTR为重要的信息你需要处理作为string(无论出于何种原因-这是节目,这样的东西会发生)

现在,如果你使用多-这不是一个问题。但是有一个办法解决它。这是我的方法是无可否认马虎。但尽管如此,你应该能看到它是如何工作的。

const std::wstring &wstring = AcctName; // AcctName being my LPTSTR string
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstring[0], (int)wstring.size(), NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);

WideCharToMultiByte(CP_UTF8, 0, & wstring[0], (int)wstring[0], &strTo[0], size_needed, NULL, NULL);

char* charUserName = new char[strTo.size() + 1];

// Set charUserName via copying
std::copy(strTo.begin(), strTo.end(), charUserName);
charUserName[strTo.size()] = '\0';

SetUPI(charUserName); // charUserName being my converted char * - 
// You don't need this last part - but this is an example of passing to method
// that takes a string

任何疑问,只是问问。我意识到这是一个古老后-但我喜欢以后为人民在未来的话题来看。(我这样的人)

我希望这可以帮助别人,因为它花了我一段时间来弄清楚如何做到这一点。

第一, LPTSTR 是的指针的类型和它基本上是相当于 TCHAR* (假设 <tchar.h> 被包括在内)。注意大小的 TCHAR 变化的基础的字符编码的类型。即如果unicode的定义, TCHAR 等于 wchar_t, ,否则就是 char.

当然,如果你把广泛角色向一个正常的 char, 你只能保持低位,并可能失去一些数据。这在某种程度上刺激我。所以我写了下列代码。其主要优点是这样做的转换不会丢失任何数据。

顺便说一句,如果您好的数据丢失,然后 wcstombs 不会的工作。

#include <cstring>
#include <algorithm>
#include <tchar.h>

void lptstr2str(LPTSTR tch, char* &pch) // or (TCHAR* tch, char* &pch)
{
#ifndef UNICODE
    std::memcpy(pch, tch, strlen(tch) + 1);
#else
    size_t n =
        sizeof(TCHAR) / sizeof(char)* wcsnlen(tch, std::string::npos);
    pch = new char[n + 1];
    std::memcpy(pch, tch, n + 1);
    int len = n - std::count(pch, pch + n, NULL);
    std::remove(pch, pch + n, NULL);
    pch[len] = NULL;
#endif
}

我是缺少一些简单的例子,所以这里是:

(对于我char*相同char[])

LPCTSTR myLPCTSTR = getLPCTSTR();
TCHAR myT[500];
wcscpy(myT,myLPCTSTR);
char myC[500];
sprintf(myC, "%S", myT);

毫无疑问,许多(例如,我们unix民)将反恐怖在疯癫Microserf口是心非-"如果你编译器是Unicode的模式,使用边或坚持一个"T_"在它前面,但仅仅如果它是一个静态的串,这是同一个"L",或者使用T2A()如果使用ATL,但是,现在已经过时,或使用的变型,但是如果没有联COM/OLE"...).

"如果(sizeof(TCHAR)==sizeof(char))"此页上列出的是一个合乎逻辑的尝试,在一个漂亮的解决方案,但是它不会compile-无论是,如果真的不会汇编,或者如果假不会'汇编,这取决于你的编译器的旗帜(第一个线索防御!).写和忘记便携式解决则需要诉诸[太通用的命名]UNICODE宏。我提供这种适应的前一个代号:

string mfc_to_zstring (CString &sref)
{
    char nojoy[65536];
    char *ptr, *psin = NULL;
    string sot;
    LPCTSTR p = sref;


#if UNICODE
    if (sizeof(TCHAR) != sizeof(char))
    {
        size_t n = wcstombs(NULL, p, 0);
        if (n > 65530)
        {
            psin = new char[n + 1];
            wcstombs(psin, p, n + 1);
            ptr = psin;
        }
        else
        {
            wcstombs(nojoy, p, n + 1);
            ptr = nojoy;
        }

        sot = ptr;
        if (psin != NULL)
            delete psin;
    }
    else
        { std::cerr << "Aaargh! Microsoft horror.\n"; exit(1); }
#else
    if (sizeof(TCHAR) == sizeof(char))
    {
        const char *ptr = p;
        sot = ptr;
    }
    else
      { std::cerr << "Aaargh! You should never see this line\n"; exit(1); }
#endif

    return sot;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top