Cstring 转CstringW 
时间: 2023-03-24 21:02:45 浏览: 35
你可以使用 MultiByteToWideChar 函数将 Cstring 转换为 CstringW。具体实现可以参考以下代码:
```
#include <windows.h>
CStringW ConvertCStringToCStringW(CString str)
{
int len = str.GetLength();
int unicodeLen = ::MultiByteToWideChar(CP_ACP, , str, len, NULL, );
CStringW wstr;
if (unicodeLen > )
{
::MultiByteToWideChar(CP_ACP, , str, len, wstr.GetBuffer(unicodeLen + 1), unicodeLen);
wstr.ReleaseBuffer();
}
return wstr;
}
```
相关问题
CString 转 int
可以使用 `atoi` 或 `stoi` 函数将 `CString` 转换为整数。
使用 `atoi` 函数:
```c++
#include <cstdlib>
#include <cstring>
// CString 转 int
int CStringToInt(CString str)
{
return std::atoi(str.GetString());
}
```
使用 `stoi` 函数:
```c++
#include <string>
// CString 转 int
int CStringToInt(CString str)
{
std::string stdStr(str.GetString());
return std::stoi(stdStr);
}
```
需要注意的是,如果 `CString` 中包含非数字字符,那么这些非数字字符会被忽略。如果 `CString` 不能被转换为整数,那么 `atoi` 返回 0,`stoi` 抛出 `std::invalid_argument` 异常。
CString 转string
CString 转 string 可以通过以下几种方法实现:
1. 使用 CString 的 GetBuffer() 和 ReleaseBuffer() 函数:
CString cs = _T("abcde");
std::string str(cs.GetBuffer(cs.GetLength()));
cs.ReleaseBuffer();
\[1\]
2. 使用 CString 的 Format() 函数:
CString cs = _T("abcde");
std::string str;
str.Format("%s", cs);
\[3\]
3. 使用 CString 的 CStringA 类型:
CString cs = _T("abcde");
std::string str(CT2A(cs));
\[2\]
请注意,在使用 GetBuffer() 函数时,需要在使用完毕后调用 ReleaseBuffer() 函数来释放缓冲区所占的空间。另外,使用 CStringA 类型可以直接将 CString 转换为 std::string 类型。
#### 引用[.reference_title]
- *1* *2* *3* [string 与 CString 转化](https://blog.csdn.net/red10057/article/details/6401420)[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 ]
相关推荐











