【转载】如何:在各种字符串类型之间进行转换

简介:

【转载】如何:在各种字符串类型之间进行转换
2008-03-19 17:36如何:在各种字符串类型之间进行转换


本主题演示如何将各种 Visual 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)

目录
相关文章
|
设计模式 前端开发 Java
DTO和VO的区别及使用场景详解
DTO和VO的区别及使用场景详解
7283 1
|
监控 安全 Linux
reactor的原理与实现
前情回顾 网络IO,会涉及到两个系统对象:   一个是用户空间调用的进程或线程   一个是内核空间的内核系统 如果发生IO操作read时,会奖励两个阶段:
376 1
|
安全 应用服务中间件 Linux
nginx搭建静态文件下载服务器
nginx搭建静态文件下载服务器
929 0
|
SQL 运维 监控
如何做数据库自动化运维
【5月更文挑战第5天】IT运维中的DBA面临诸多挑战,包括库表设计规范落地困难、SQL审核繁琐、数据提取需求频繁、资源管理和监控复杂。为解决这些问题,引入数据库自动化运维平台至关重要。该平台实现SQL审核自动化,确保语句规范和安全,支持数据提取的自助服务,强化元数据管理,提供一键资源部署,并进行全面的数据库监控。这样,DBA的角色转变为平台管理者,提高效率,规范化流程,减轻工作负担。
|
关系型数据库 MySQL 数据库
MySQL忘记密码的处理方法(MySQL重置密码)
本文主要讲解MySQL如何重置密码(MySQL密码重置方法)
91735 2
MySQL忘记密码的处理方法(MySQL重置密码)
|
监控 安全 网络安全
接口频繁请求,被刷爆怎么办
接口频繁请求,被刷爆怎么办
R语言用回归构建配对交易(Pairs Trading)策略量化模型分析股票收益和价格
R语言用回归构建配对交易(Pairs Trading)策略量化模型分析股票收益和价格
|
缓存 负载均衡 前端开发
SpringCloud之OpenFeign实现服务间请求头数据传递(OpenFeign拦截器RequestInterceptor的使用)
SpringCloud之OpenFeign实现服务间请求头数据传递(OpenFeign拦截器RequestInterceptor的使用)
4586 0
SpringCloud之OpenFeign实现服务间请求头数据传递(OpenFeign拦截器RequestInterceptor的使用)
|
安全 Java Linux
从0开始,让你的Spring Boot项目跑在Linux服务器
让你的Spring Boot项目跑在Linux服务器
1277 0
从0开始,让你的Spring Boot项目跑在Linux服务器
|
SQL Java 数据库连接
mybatis采坑之 PageHelper.startPage出现两个limit情况
首先这个问题是在我们做项目迁移的时候遇到的,在新写的接口中最开始的去调用接口总是没有问题的,但是如果一直调用这个接口,发现会出现数据的丢失