stdstring、LPCWCH、LPSTR之间转换

std::string、LPCWCH、LPSTR之间转换

最近在写一个程序,用到了windows 原始API,遇到了字符之间的转换问题,在网上发现,都不是很全面。所以整理一下源码,记录一下,希望帮到有需要的人。

/*******************************************************************************
*  Function   : LPCWCH2LPSTR
*  Description: LPCWCH转换LPSTR
*  Calls      : 见函数实现
*  Called By  :
*  Input      :
*  Output     :
*  Return     :
*  Others     :
********************************************************************************/
LPSTR LPCWCH2LPSTR(const LPCWCH strCS)
{
	const UINT wLen = lstrlen(strCS) + 1;
	UINT aLen = WideCharToMultiByte(CP_ACP, 0, strCS, wLen, NULL, 0, NULL, NULL);
	LPSTR lpa = new char[aLen+1];
    memset(lpa, 0, aLen+1);
	WideCharToMultiByte(CP_ACP, 0, strCS, wLen, lpa, aLen, NULL, NULL);

	return lpa;
}
/*******************************************************************************
*  Function   : LPCWCH2STDString
*  Description: LPCWCH转换LPSTR
*  Calls      : 见函数实现
*  Called By  :
*  Input      :
*  Output     :
*  Return     :
*  Others     :
********************************************************************************/
std::string LPCWCH2STDString(const LPCWCH strcs)
{
	LPSTR ltmp = LPCWCH2LPSTR(strcs);
	std::string stdStr(ltmp);
	delete[] ltmp;

	return stdStr;
}

/*******************************************************************************
*  Function   : STDString2LPWSTR
*  Description: std::string转换LPWSTR
*  Calls      : 见函数实现
*  Called By  :
*  Input      :
*  Output     :
*  Return     :
*  Others     :
********************************************************************************/
LPWSTR STDString2LPCWCH(const std::string strcs)
{
    UINT len = MultiByteToWideChar(CP_ACP, 0, strcs.c_str(), -1, NULL, 0);
    LPWSTR  wszasc = new WCHAR[len+1];
	MultiByteToWideChar(CP_ACP, 0, strcs.c_str(), -1, wszasc, len);
    return wszasc;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是TCHAR和std::string之间转换方法: 1.将TCHAR转换为std::string: ```c++ #include <string> #include <tchar.h> TCHAR tcharStr[] = _T("TCHAR string"); std::string str = std::string(tcharStr, tcharStr + _tcslen(tcharStr)); ``` 2.将std::string转换为TCHAR: ```c++ #include <string> #include <tchar.h> std::string str = "std::string"; TCHAR tcharStr[100]; _tcscpy_s(tcharStr, CA2T(str.c_str())); ``` 其中,CA2T是ATL/MFC库中的一个宏,用于将const char*转换为TCHAR*。 以下是std::string和LPCWSTR之间转换方法: 1.将std::string转换为LPCWSTR: ```c++ #include <string> #include <Windows.h> std::string str = "std::string"; LPCWSTR lpcwstr = CA2W(str.c_str()); ``` 其中,CA2W是ATL/MFC库中的一个宏,用于将const char*转换为LPCWSTR。 2.将LPCWSTR转换为std::string: ```c++ #include <string> #include <Windows.h> LPCWSTR lpcwstr = L"LPCWSTR string"; std::wstring wstr(lpcwstr); std::string str(wstr.begin(), wstr.end()); ``` 以下是完整的代码示例: ```c++ #include <iostream> #include <string> #include <tchar.h> #include <Windows.h> using namespace std; int main() { // TCHAR to std::string TCHAR tcharStr[] = _T("TCHAR string"); std::string str1 = std::string(tcharStr, tcharStr + _tcslen(tcharStr)); cout << "TCHAR to std::string: " << str1 << endl; // std::string to TCHAR std::string str2 = "std::string"; TCHAR tcharStr2[100]; _tcscpy_s(tcharStr2, CA2T(str2.c_str())); wcout << "std::string to TCHAR: " << tcharStr2 << endl; // std::string to LPCWSTR std::string str3 = "std::string"; LPCWSTR lpcwstr = CA2W(str3.c_str()); wcout << "std::string to LPCWSTR: " << lpcwstr << endl; // LPCWSTR to std::string LPCWSTR lpcwstr2 = L"LPCWSTR string"; std::wstring wstr(lpcwstr2); std::string str4(wstr.begin(), wstr.end()); cout << "LPCWSTR to std::string: " << str4 << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

匠心码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值