TCHAR 、CString 、LPCTSTR、char*、LPWSTR、char []、wchar_t *、string之间的转换

一、TCHAR 、CString 、LPCTSTR转换

TCHAR  tc[MAX_PATH] = { 0 };

wcout <<"TCHAR 转换成LPCTSTR类型=="<< (LPCTSTR)tc<<endl;

CString cs= tc;

wcout <<"TCHAR 转换成CString类型=="<< cs<<endl;

 

二、CString转换成char*

前面的法1、法2、法3当打印时可能只获取到首字母,因为版本改变了。这是法4能完美解决,建议用法4的方法。

CString strSource;//CString

char* charSource; //char*
法1:
charSource = (char*)strSource.GetBuffer(0);
法2:
charSource = (char*)strSource.GetBuffer(strSource.GetLength());
法3:
charSource = (char*)(LPCTSTR)strSource;

法4:

 char   *buf   = ConvertLPWSTRToLPSTR(strSource.GetBuffer());

char* ConvertLPWSTRToLPSTR(LPWSTR lpwszStrIn)
{
    LPSTR pszOut = NULL;
    try
    {
        if (lpwszStrIn != NULL)
        {
            int nInputStrLen = wcslen(lpwszStrIn);

            // Double NULL Termination  
            int nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;
            pszOut = new char[nOutputStrLen];

            if (pszOut)
            {
                memset(pszOut, 0x00, nOutputStrLen);
                WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
            }
        }
    } catch (std::exception e) {}

    return pszOut;
}
三、char [] 和char*和wchar_t*和LPWSTR之间转换

法1:

#include <atlconv.h>
USES_CONVERSION;  
char sz[] = "我是多字节string";
wchar_t *pWChar = A2W(sz);  
 LPWSTR lp = pWChar ;

char* buffer="123";

LPWSTR str=A2W(buffer);

LPCWSTR c_st=A2W(buffer);


wchar_t wsz[] = L"我是宽字符string";
char *pChar = W2A(wsz);

法2:

string str1 = "你好,我是w";
LPWSTR lp = ConvertCharToLPWSTR(str1.c_str());

LPWSTR ConvertCharToLPWSTR(const char * szString)
{
    int dwLen = strlen(szString) + 1;
    int nwLen = MultiByteToWideChar(CP_ACP, 0, szString, dwLen, NULL, 0);//算出合适的长度
    LPWSTR lpszPath = new WCHAR[dwLen];
    MultiByteToWideChar(CP_ACP, 0, szString, dwLen, lpszPath, nwLen);
    return lpszPath;
}

四、std::string 转LPCSTR

LPCTSTR不是一个类型,而是两种类型:LPCSTR和LPCWSTR其中之一。会根据你当前程序是否使用UNICODE字符集来变成那二者之一。如果使用UNICODE字符集,则LPCTSTR = LPCWSTR,否则LPCTSTR = LPCSTR。

标准库的std::string转换成LPCSTR很简单:直接调用c_str()即可。例:
std::string a="abc"; 
LPCSTR str = a.c_str();

标准库还有一个wstring,代表宽字符的string,std::wstring转换成LPCWSTR也一样很简单:
std::wstring a = L"abc";
LPCWSTR str = a.c_str();

如果要是std::string转换成LPCWSTR或者std::wstring转换成LPCSTR那就比较麻烦了,需要调用MultiByteToWideChar或WideCharToMultiByte进行字符集之间的转换。不过大多数时候不需要这种交叉转换,一个程序一般只会使用一种字符集。
 

在使用VS Code时,出现 `cannot convert TCHAR* {aka char*} to const wchar_t*` 错误,这是因为在编译过程中涉及到了字符编码的转换问题。TCHAR是一种根据字符集定义的宏,根据不同的配置,它可以表示char类型或wchar_t类型。 对于此错误,可能是由于将一个TCHAR*(char*)类型的变量传递给一个期望接收const wchar_t*类型参数的函数或方法导致的。解决这个问题的方法之一是进行显示的字符编码转换。 你可以使用`MultiByteToWideChar`函数将char*类型的字符串转换为wchar_t*类型。这个函数可以在Windows API中找到,它可以将多字节字符转换为宽字符。以下是一个简单的示例代码: ```cpp #include <windows.h> wchar_t* ConvertToWideChar(const char* str) { int bufferSize = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); wchar_t* wideCharStr = new wchar_t[bufferSize]; MultiByteToWideChar(CP_UTF8, 0, str, -1, wideCharStr, bufferSize); return wideCharStr; } int main() { const char* narrowStr = "Hello, World!"; wchar_t* wideStr = ConvertToWideChar(narrowStr); // 使用wideStr进行操作 delete[] wideStr; return 0; } ``` 在这个示例中,`ConvertToWideChar`函数将一个char*类型的字符串转换为wchar_t*类型的字符串,并返回转换后的宽字符字符串。你可以将此宽字符字符串传递给接受const wchar_t*类型参数的函数。 希望这个解决方案对你有帮助!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值