如何从 CString 转换为其他字符串类型
2011-12-19 15:57
323 查看
// convert_from_cstring.cpp // compile with: /clr /link comsuppw.lib #include <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "comutil.h" using namespace std; using namespace System; int main() { CString orig("Hello, World!"); wcout << orig << " (CString)" << endl; // Convert to a char* const size_t newsize = 100; char nstring[newsize]; strcpy_s(nstring, orig); strcat_s(nstring, " (char *)"); cout << nstring << endl; // Convert to a wchar_t* // You must first convert to a char * for this to work. size_t origsize = strlen(orig) + 1; size_t convertedChars = 0; wchar_t wcstring[newsize]; mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE); wcscat_s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; // Convert to a _bstr_t _bstr_t bstrt(orig); bstrt += " (_bstr_t)"; cout << bstrt << endl; // Convert to a CComBSTR CComBSTR ccombstr(orig); if (ccombstr.Append(L" (CComBSTR)") == S_OK) { CW2A printstr(ccombstr); cout << printstr << endl; } // Convert to a basic_string string basicstring(orig); basicstring += " (basic_string)"; cout << basicstring << endl; // Convert to a System::String String ^systemstring = gcnew String(orig); systemstring += " (System::String)"; Console::WriteLine("{0}", systemstring); delete systemstring; }
相关文章推荐
- 如何从 wchar_t * 转换为其他字符串类型
- CString 操作指南(CString与其他字符串类型转换)
- QString, wchar_t *, TCHAR, CString和其他字符或字符串类型的转换
- 如何将CString 的一个字符串转换成一个string 类型的
- VARIANT类型如何与其他字符串类型转换?
- 如何:在各种字符串类型之间进行转换--如何从 char * 转换为上面列出的其他字符串类型
- 数据类型转字符串及CString与其他数据类型的转换
- 如何从 basic_string 转换为其他字符串类型
- 笔记:MFC中CString和其他类型的转换
- C++ 如何:在各种字符串类型之间进行转换
- 如何正确转换QSTring类型字符串和UNICODE
- js中如何将字符串转换成json类型
- Visual C++ 如何:在各种字符串类型之间进行转换
- Visual C++ 如何:在各种字符串类型之间进行转换
- CString与其他类型转换
- CString类型转换,字符串操作和UNICODE、ANSI字符集
- Swift3字符串转换为其他数据类型
- MFC中其他数据类型转换为CString类型
- CString和其他数据类型之间的转换!
- QString, wchar_t *, TCHAR, CString和其他字符或字符串类型的