c++ string wstring
时间: 2023-08-20 07:05:53 浏览: 51
要将一个`wstring`转换成`string`,可以使用`WideCharToMultiByte`函数来实现。这个函数可以将Unicode字符串转换成多字节字符串。你需要提供源字符串的编码格式,目标字符串的编码格式以及一些其他的参数。在C++中,你可以使用以下代码来完成转换:
```cpp
#include <iostream>
#include <windows.h>
std::string wstringToString(const std::wstring& wstr, UINT codePage) {
int size = WideCharToMultiByte(codePage, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
std::string str(size, 0);
WideCharToMultiByte(codePage, 0, wstr.c_str(), -1, &str<span class="em">1</span>
#### 引用[.reference_title]
- *1* [wstring转string](https://download.csdn.net/download/rabbit_wuxiao/8063827)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]