本主题演示如何将各种 C++ 字符串类型转换为其他字符串。可以转换的字符串类型包括 char *、wchar_t*、_bstr_t、CComBSTR、CString、basic_string 和 System.String。在所有情况下,在将字符串转换为新类型时,都会创建字符串的副本。对新字符串进行的任何更改都不会影响原始字符串,反之亦然。
从 char * 转换
示例
说明
此示例演示如何从 char * 转换为上面列出的其他字符串类型。
// convert_from_char.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(){ char *orig = "Hello, World!"; cout << orig << " (char *)" << endl; // Convert to a wchar_t* size_t origsize = strlen(orig) + 1; const size_t newsize = 100; 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 CString CString cstring(orig); cstring += " (CString)"; cout << cstring << 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;}
输出
Hello, World! (char *)Hello, World! (wchar_t *)Hello, World! (_bstr_t)Hello, World! (CComBSTR)Hello, World! (CString)Hello, World! (basic_string)Hello, World! (System::String)
从 wchar_t * 转换
示例
说明
此示例演示如何从 wchar_t * 转换为上面列出的其他字符串类型。
// convert_from_wchar_t.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(){ wchar_t *orig = L"Hello, World!"; wcout << orig << L" (wchar_t *)" << endl; // Convert to a char* size_t origsize = wcslen(orig) + 1; const size_t newsize = 100; size_t convertedChars = 0; char nstring[newsize]; wcstombs_s(&convertedChars, nstring, origsize, orig, _TRUNCATE); strcat_s(nstring, " (char *)"); cout << nstring << 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 CString CString cstring(orig); cstring += " (CString)"; cout << cstring << endl; // Convert to a basic_string wstring basicstring(orig); basicstring += L" (basic_string)"; wcout << basicstring << endl; // Convert to a System::String String ^systemstring = gcnew String(orig); systemstring += " (System::String)"; Console::WriteLine("{0}", systemstring); delete systemstring;}
输出
Hello, World! (wchar_t *)Hello, World! (char *)Hello, World! (_bstr_t)Hello, World! (CComBSTR)Hello, World! (CString)Hello, World! (basic_string)Hello, World! (System::String)
从 _bstr_t 转换
示例
说明
此示例演示如何从 _bstr_t 转换为上面列出的其他字符串类型。
// convert_from_bstr_t.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(){ _bstr_t orig("Hello, World!"); wcout << orig << " (_bstr_t)" << endl; // Convert to a char* const size_t newsize = 100; char nstring[newsize]; strcpy_s(nstring, (char *)orig); strcat_s(nstring, " (char *)"); cout << nstring << endl; // Convert to a wchar_t* wchar_t wcstring[newsize]; wcscpy_s(wcstring, (wchar_t *)orig); wcscat_s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; // Convert to a CComBSTR CComBSTR ccombstr((char *)orig); if (ccombstr.Append(L" (CComBSTR)") == S_OK) { CW2A printstr(ccombstr); cout << printstr << endl; } // Convert to a CString CString cstring((char *)orig); cstring += " (CString)"; cout << cstring << endl; // Convert to a basic_string string basicstring((char *)orig); basicstring += " (basic_string)"; cout << basicstring << endl; // Convert to a System::String String ^systemstring = gcnew String((char *)orig); systemstring += " (System::String)"; Console::WriteLine("{0}", systemstring); delete systemstring;}
输出
Hello, World! (_bstr_t)Hello, World! (char *)Hello, World! (wchar_t *)Hello, World! (CComBSTR)Hello, World! (CString)Hello, World! (basic_string)Hello, World! (System::String)
从 CComBSTR 转换
示例
说明
此示例演示如何从 CComBSTR 转换为上面列出的其他字符串类型。
// convert_from_ccombstr.cpp// compile with /clr /link comsuppw.lib#include <iostream>#include <stdlib.h>#include <string>#include "atlbase.h"#include "atlstr.h"#include "comutil.h"#include "vcclr.h"using namespace std;using namespace System;using namespace System::Runtime::InteropServices;int main(){ CComBSTR orig("Hello, World!"); CW2A printstr(orig); cout << printstr << " (CComBSTR)" << endl; // Convert to a char* const size_t newsize = 100; char nstring[newsize]; CW2A tmpstr1(orig); strcpy_s(nstring, tmpstr1); strcat_s(nstring, " (char *)"); cout << nstring << endl; // Convert to a wchar_t* wchar_t wcstring[newsize]; wcscpy_s(wcstring, orig); 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 CString CString cstring(orig); cstring += " (CString)"; cout << cstring << endl; // Convert to a basic_string wstring basicstring(orig); basicstring += L" (basic_string)"; wcout << basicstring << endl; // Convert to a System::String String ^systemstring = gcnew String(orig); systemstring += " (System::String)"; Console::WriteLine("{0}", systemstring); delete systemstring;}
输出
Hello, World! (CComBSTR)Hello, World! (char *)Hello, World! (wchar_t *)Hello, World! (_bstr_t)Hello, World! (CString)Hello, World! (basic_string)Hello, World! (System::String)
从 CString 转换
示例
说明
此示例演示如何从 CString 转换为上面列出的其他字符串类型。
// 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;}
输出
Hello, World! (CString)Hello, World! (char *)Hello, World! (wchar_t *)Hello, World! (_bstr_t)Hello, World! (CComBSTR)Hello, World! (basic_string)Hello, World! (System::String)
从 basic_string 转换
示例
说明
此示例演示如何从 basic_string 转换为上面列出的其他字符串类型。
// convert_from_basic_string.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(){ string orig("Hello, World!"); cout << orig << " (basic_string)" << endl; // Convert to a char* const size_t newsize = 100; char nstring[newsize]; strcpy_s(nstring, orig.c_str()); 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.c_str()) + 1; size_t convertedChars = 0; wchar_t wcstring[newsize]; mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE); wcscat_s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; // Convert to a _bstr_t _bstr_t bstrt(orig.c_str()); bstrt += " (_bstr_t)"; cout << bstrt << endl; // Convert to a CComBSTR CComBSTR ccombstr(orig.c_str()); if (ccombstr.Append(L" (CComBSTR)") == S_OK) { CW2A printstr(ccombstr); cout << printstr << endl; } // Convert to a CString CString cstring(orig.c_str()); cstring += " (CString)"; cout << cstring << endl; // Convert to a System::String String ^systemstring = gcnew String(orig.c_str()); systemstring += " (System::String)"; Console::WriteLine("{0}", systemstring); delete systemstring;}
输出
Hello, World! (basic_string)Hello, World! (char *)Hello, World! (wchar_t *)Hello, World! (_bstr_t)Hello, World! (CComBSTR)Hello, World! (CString)Hello, World! (System::String)
从 System::String 转换
示例
说明
此示例演示如何从 System.String 转换为上面列出的其他字符串类型。
// convert_from_system_string.cpp// compile with /clr /link comsuppw.lib#include <iostream>#include <stdlib.h>#include <string>#include "atlbase.h"#include "atlstr.h"#include "comutil.h"#include "vcclr.h"using namespace std;using namespace System;using namespace System::Runtime::InteropServices;int main(){ String ^orig = gcnew String("Hello, World!"); Console::WriteLine("{0} (System::String)", orig); pin_ptr<const wchar_t> wch = PtrToStringChars(orig); // Convert to a char* size_t origsize = wcslen(wch) + 1; const size_t newsize = 100; size_t convertedChars = 0; char nstring[newsize]; wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE); strcat_s(nstring, " (char *)"); cout << nstring << endl; // Convert to a wchar_t* wchar_t wcstring[newsize]; wcscpy_s(wcstring, wch); wcscat_s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; // Convert to a _bstr_t _bstr_t bstrt(wch); bstrt += " (_bstr_t)"; cout << bstrt << endl; // Convert to a CComBSTR CComBSTR ccombstr(wch); if (ccombstr.Append(L" (CComBSTR)") == S_OK) { CW2A printstr(ccombstr); cout << printstr << endl; } // Convert to a CString CString cstring(wch); cstring += " (CString)"; cout << cstring << endl; // Convert to a basic_string wstring basicstring(wch); basicstring += L" (basic_string)"; wcout << basicstring << endl; delete orig;}
输出
Hello, World! (System::String)Hello, World! (char *)Hello, World! (wchar_t *)Hello, World! (_bstr_t)Hello, World! (CComBSTR)Hello, World! (CString)Hello, World! (basic_string)
分享到:
相关推荐
在Visual C++.NET的所有编程方式中,我们常常要用到这样的一些基本字符串类型,如BSTR、LPSTR和LPWSTR等。之所以出现类似上述的这些数据类型,是因为不同编程语言之间的数据交换以及对ANSI、Unicode和多字节字符集...
0087 _bstr_与其他字符串类型的相互转换 37 0088 _VARIANT类型的初始化 37 0089 _variant_t与字符串类型的转换 38 0090 ColeVariant与其他数据类型的转换 38 0091 格式化类型 39 2.2 类型转换 39 0092 ...
2.2.1 使用MSComm控件发送与接收字符串 27 2.2.2 使用MSComm控件发送与接收二进制数据 27 2.3 MSComm错误处理方法 28 2.3.1 关于发送缓冲区 28 2.3.2 关于接收缓冲区 29 2.3.3 接收数据的实际处理方法 29 2.4 控件...
本书延续了Ivor Horton讲解编程语言的独特方法,从中读者可以学习Visual C++ 2005的基础知识,并全面掌握在MFC和Windows Forms中访问数据源的技术。此外,本书各章后面的习题将有助于读者温故而知新,并尽快成为C++...
Visual C++ 2005入门经典.pdf(整理并添加所有书签) ,看书的时候更方便. 封面 目录 第1章 使用Visual C++ 2005 编程 1.1 .NET Framework 1.2 CLR 1.3 编写C++应用程序 1.4 学习windows编程 1.4.1 学习c++ 1.4.2 C++...
Visual C++ 2005入门经典.pdf(整理并添加所有书签) ,看书的时候更方便. 封面 目录 第1章 使用Visual C++ 2005 编程 1.1 .NET Framework 1.2 CLR 1.3 编写C++应用程序 1.4 学习windows编程 1.4.1 学习c++ 1.4.2 C++...
Visual C++ 2005入门经典.pdf(整理并添加所有书签) ,看书的时候更方便. 封面 目录 第1章 使用Visual C++ 2005 编程 1.1 .NET Framework 1.2 CLR 1.3 编写C++应用程序 1.4 学习windows编程 1.4.1 学习c++ 1.4.2 C++...
192 如何对字符串进行比较? 193 如何从字符串中存取字符? 194 如何计算一个字符串的大小? 195 如何快速格式化一个字符串? 196 如何将CString类型转换成int类型? l97 如何将CString类型转换成loat类型? 198 如何将...
实例283 在查询语句中使用字符串函数 实例284 在查询中使用日期函数 实例285 在查询时为表中的空字段添加默认信息 第10章 打印与报表技术 10.1 基础报表打印 实例286 基于文档/视图结构的打印 实例287 基于...
3.如何在消息框中动态显示资源字符串 4.如何设置消息提示框的标题文字 5.如何使消息提示框的标题右对齐 6.如何在消息提示框中绘制表格 7.如何在消息提示框中设置图标 8.如何在消息提示框中显示问号图标 9.如何在消息...
《visual c++ 2010入门经典(第5版)》针对visual c++ 2010版本进行了全面更新,介绍了最新的开发环境和如何使用visual c++构建现实世界中的应用程序。拥有本书,您就迈向了通往使用两种c++版本编写应用程序的成功之路...
实例016——CString字符串的类型转换 实例017——获取当前的日期、时间并格式化输出 实例018——计算某日为星期几 实例019——计算两个时间点的时间间隔 实例020——使用CStringArray类创建和使用字符串数组 ...
cc实例237 利用查询语句复制表结构 9.2 查询控件 cc实例238 查询指定控件内字符串 cc实例239 查询日期控件内数据 cc实例240 控件作为字段.c操作符和内容进行查询 cc实例241 巧妙获取年龄 cc实例242 ...
实例016——CString字符串的类型转换 实例017——获取当前的日期、时间并格式化输出 实例018——计算某日为星期几 实例019——计算两个时间点的时间间隔 实例020——使用CStringArray类创建和使用字符串数组 ...
说明:本文纯粹是总结一下有关类型转换的贴子, 本人并未对所有方法都做测试,...一、其它数据类型转换为字符串 短整型(int) itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制 itoa(i,temp,2); /
cc实例238 查询指定控件内字符串 cc实例239 查询日期控件内数据 cc实例240 控件作为字段.c操作符和内容进行查询 cc实例241 巧妙获取年龄 cc实例242 格式化金额 cc实例243 如何随机显示...
cc实例238 查询指定控件内字符串 cc实例239 查询日期控件内数据 cc实例240 控件作为字段.c操作符和内容进行查询 cc实例241 巧妙获取年龄 cc实例242 格式化金额 cc实例243 如何随机显示...