本主题演示如何将各种 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 #include #include #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 *)/
欢迎您使用http://Blogmove.cn提供的"博客搬家"和"博文三窟"服务.