WCHAR如何转string
时间: 2023-09-07 11:11:49 浏览: 90
WCHAR转换为string可以通过使用WideCharToMultiByte函数来实现。这个函数可以将宽字符字符串转换为多字节字符串。下面是一个示例代码,演示了如何将WCHAR转换为string:
```cpp
#include <string>
#include <Windows.h>
std::string WCHARToString(const wchar_t* pwszSrc) {
int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
if (nLen <= 0) return std::string("");
char* pszDst = new char\[nLen\];
if (NULL == pszDst) return std::string("");
WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
pszDst\[nLen - 1\] = 0;
std::string strTmp(pszDst);
delete\[\] pszDst;
return strTmp;
}
```
你可以使用上述函数将WCHAR转换为string。只需将WCHAR字符串作为参数传递给WCHARToString函数,它将返回相应的string类型结果。
#### 引用[.reference_title]
- *1* [wchar_t、char 、string、 wstring 类型相互转换](https://blog.csdn.net/weixin_43896283/article/details/126689638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [【C++】WCHAR 转换为 std::string](https://blog.csdn.net/kingkee/article/details/98115024)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [wchar* 转换成 string](https://blog.csdn.net/qq_34842360/article/details/102365842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文