本主题演示如何将各种 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)
分享到:
相关推荐
本文将详细介绍如何在各种C++字符串类型之间进行转换,包括`char *`、`wchar_t*`、`_bstr_t`、`CComBSTR`、`CString`、`basic_string`以及`System.String`。这些转换对于跨平台或跨API交互时特别有用,因为不同的库...
以下是如何在各种C++字符串类型之间进行转换的知识点: 1. **`char *`到其他类型的转换**: - 使用`mbstowcs_s`函数将`char *`(多字节字符)转换为`wchar_t *`(宽字符)字符串。此函数会考虑源字符串的大小,并...
本文主要探讨了如何在各种C++字符串类型之间进行转换,包括`char *`、`wchar_t*`、`_bstr_t`、`CComBSTR`、`CString`、`std::basic_string`以及.NET框架中的`System.String`。以下是对这些转换的详细说明: 1. **从...
在Visual C++.NET中,开发人员常常需要处理各种不同编码的字符串类型,如ANSI、Unicode和BSTR。本文将深入探讨这些字符串类型及其转换方法。 首先,BSTR(Basic String)是一种Unicode字符串,它是COM(Component ...
首先,我们来了解C++中的基本字符串类型——`std::string`。`std::string`是C++标准库中的一个类,它代表可变长度的字符串。相比于C语言中的字符数组,`std::string`提供了更安全、更方便的字符串操作方式。例如,...
本文将详细介绍托管C++(Managed C++)与非托管C++(Unmanaged C++)之间各种字符串类型转换的方法与技巧。虽然从表面上看这些转换操作似乎很简单,但实际上涉及到的技术细节和背景知识相当丰富。通过本文的学习,...
在C++编程环境中,Visual Studio 2008(VS2008)是一个常用的开发工具,它提供了丰富的库和功能来支持字符串和数字之间的转换。在这个主题中,我们将深入探讨如何在C++中实现字符串到数字以及数字到字符串的转换。 ...
在本篇内容中,我们将通过一个具体的C++课程设计案例来深入理解如何进行字符串操作。案例选自《Visual C++课程设计 案例精选与编程指导》一书中的第3.7节“字符串操作”。 #### 二、案例背景 本案例旨在帮助读者...
在提供的压缩包文件"visual c++字符串运用 各种代码"中,可能包含了以上各种字符串操作的实例代码,通过学习和理解这些代码,可以加深对Visual C++中字符串处理的理解和应用技巧。实践是最好的老师,通过分析和运行...
在C++编程中,将JSON字符串转换为C++类对象是一项常见的任务,特别是在处理网络通信、数据存储或配置文件时。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析...
在不同的字符串类型之间进行转换是非常常见的需求,特别是在处理国际化应用时。例如,将`std::string`转换为`std::wstring`: ```cpp std::string str = "Hello"; std::wstring wstr(str.begin(), str.end()); ``` ...
《C++字符串完全指南—第二部分:字符串的封装类》主要探讨了在C++中处理字符串的各种封装类,包括在Win32 API、MFC、STL、WTL和Visual C++运行时库中常见的字符串类型。这些封装类旨在解决C风格字符串(C-style ...
在C++编程中,将二进制字符串转换为十进制数是一项常见的任务,尤其是在处理计算机数据表示或者进行位操作时。在这个问题中,我们主要关注如何在Visual C++环境下实现这一转换。以下是一个详细的过程解释及代码示例...
因此,传递字符串数组时,需要进行适当的类型转换。 1. **定义C++ DLL接口**: 在C++ DLL中,我们需要创建一个函数来接收字符串数组。由于C++不支持数组作为参数,通常我们会使用指针和数组长度作为参数。例如,...
在IT领域,编程语言的选择与理解其数据类型是至关重要的,因为不同的编程语言对数据类型的定义和处理方式可能有所...通过上述对照表,开发者可以更好地在不同语言之间进行转换和通信,确保数据的准确性和程序的性能。
首先,我们要了解VC++中的两种主要的字符串类型:`std::string`(来自C++标准库)和`TCHAR`相关的字符串类型(如`LPCTSTR`、`LPTSTR`等),它们在不同的上下文中有不同的用法。`std::string`适用于纯C++环境,而`...
在C++编程中,字符串的编码转换是一个关键的议题,特别是在多平台开发和跨语言交互时。本文档主要探讨了C++编译器如何处理不同的字符串编码,包括ANSI字符串和Unicode(Wide字符串)的转换。以下是针对不同编译器的...
- `.rc`:资源脚本文件,用于定义应用程序的各种资源,如菜单、对话框、图标、字符串等。 - `.res`:编译后的资源文件,由.rc文件编译得到,可直接链接到可执行文件中。 5. **配置和设置文件**: - `.props`:...
12. **国际化和本地化**:Visual C++支持创建支持多种语言的应用,包括字符串资源的翻译和日期时间格式的处理。 通过《Visual C++语言参考手册》,开发者可以深入理解Visual C++的各个方面,从而更好地利用这一强大...