C++ 字符类型互相转换[转]

转自 

C++ 字符串类型转换(Char *、Wchar_t*、_bstr_t、CComBSTR、CString、Basic_string 和 System.String)

在C++中经常涉及字符串类型转换,VC++里面尤其严重,实在搞不通微软为什么要搞这么多的字符串类型,实在是纠结,这里做一个简单的小总结:

从 char * 转换

示例

说明

此示例演示如何从 char * 转换为上面列出的其他字符串类型。

  1. // convert_from_char.cpp
  2. // compile with /clr /link comsuppw.lib
  3.  
  4. #include<iostream>
  5. #include<stdlib.h>
  6. #include<string>
  7.  
  8. #include"atlbase.h"
  9. #include"atlstr.h"
  10. #include"comutil.h"
  11.  
  12. usingnamespace std;
  13. usingnamespaceSystem;
  14.  
  15. int main()
  16. {
  17. char*orig ="Hello, World!";
  18. cout << orig <<" (char *)"<< endl;
  19.  
  20. // Convert to a wchar_t*
  21. size_t origsize = strlen(orig)+1;
  22. constsize_t newsize =100;
  23. size_t convertedChars =0;
  24. wchar_t wcstring[newsize];
  25. mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
  26. wcscat_s(wcstring, L" (wchar_t *)");
  27. wcout << wcstring << endl;
  28.  
  29. // Convert to a _bstr_t
  30. _bstr_t bstrt(orig);
  31. bstrt +=" (_bstr_t)";
  32. cout << bstrt << endl;
  33.  
  34. // Convert to a CComBSTR
  35. CComBSTR ccombstr(orig);
  36. if(ccombstr.Append(L" (CComBSTR)")== S_OK)
  37. {
  38. CW2A printstr(ccombstr);
  39. cout << printstr << endl;
  40. }
  41.  
  42. // Convert to a CString
  43. CString cstring(orig);
  44. cstring +=" (CString)";
  45. cout << cstring << endl;
  46.  
  47. // Convert to a basic_string
  48. string basicstring(orig);
  49. basicstring +=" (basic_string)";
  50. cout << basicstring << endl;
  51.  
  52. // Convert to a System::String
  53. String^systemstring = gcnew String(orig);
  54. systemstring +=" (System::String)";
  55. Console::WriteLine("{0}", systemstring);
  56. delete systemstring;
  57. }

输出

  1. Hello,World!(char*)
  2. Hello,World!(wchar_t*)
  3. Hello,World!(_bstr_t)
  4. Hello,World!(CComBSTR)
  5. Hello,World!(CString)
  6. Hello,World!(basic_string)
  7. Hello,World!(System::String)

从 wchar_t * 转换

示例

说明

此示例演示如何从 wchar_t * 转换为上面列出的其他字符串类型。

  1. // convert_from_wchar_t.cpp
  2. // compile with /clr /link comsuppw.lib
  3.  
  4. #include<iostream>
  5. #include<stdlib.h>
  6. #include<string>
  7.  
  8. #include"atlbase.h"
  9. #include"atlstr.h"
  10. #include"comutil.h"
  11.  
  12. usingnamespace std;
  13. usingnamespaceSystem;
  14.  
  15. int main()
  16. {
  17. wchar_t*orig = L"Hello, World!";
  18. wcout << orig << L" (wchar_t *)"<< endl;
  19.  
  20. // Convert to a char*
  21. size_t origsize = wcslen(orig)+1;
  22. constsize_t newsize =100;
  23. size_t convertedChars =0;
  24. char nstring[newsize];
  25. wcstombs_s(&convertedChars, nstring, origsize, orig, _TRUNCATE);
  26. strcat_s(nstring," (char *)");
  27. cout << nstring << endl;
  28.  
  29. // Convert to a _bstr_t
  30. _bstr_t bstrt(orig);
  31. bstrt +=" (_bstr_t)";
  32. cout << bstrt << endl;
  33.  
  34. // Convert to a CComBSTR
  35. CComBSTR ccombstr(orig);
  36. if(ccombstr.Append(L" (CComBSTR)")== S_OK)
  37. {
  38. CW2A printstr(ccombstr);
  39. cout << printstr << endl;
  40. }
  41.  
  42. // Convert to a CString
  43. CString cstring(orig);
  44. cstring +=" (CString)";
  45. cout << cstring << endl;
  46.  
  47. // Convert to a basic_string
  48. wstring basicstring(orig);
  49. basicstring += L" (basic_string)";
  50. wcout << basicstring << endl;
  51.  
  52. // Convert to a System::String
  53. String^systemstring = gcnew String(orig);
  54. systemstring +=" (System::String)";
  55. Console::WriteLine("{0}", systemstring);
  56. delete systemstring;
  57. }

输出

  1. Hello,World!(wchar_t*)
  2. Hello,World!(char*)
  3. Hello,World!(_bstr_t)
  4. Hello,World!(CComBSTR)
  5. Hello,World!(CString)
  6. Hello,World!(basic_string)
  7. Hello,World!(System::String)

从 _bstr_t 转换

示例

说明

此示例演示如何从 _bstr_t 转换为上面列出的其他字符串类型。

  1. // convert_from_bstr_t.cpp
  2. // compile with /clr /link comsuppw.lib
  3.  
  4. #include<iostream>
  5. #include<stdlib.h>
  6. #include<string>
  7.  
  8. #include"atlbase.h"
  9. #include"atlstr.h"
  10. #include"comutil.h"
  11.  
  12. usingnamespace std;
  13. usingnamespaceSystem;
  14.  
  15. int main()
  16. {
  17. _bstr_t orig("Hello, World!");
  18. wcout << orig <<" (_bstr_t)"<< endl;
  19.  
  20. // Convert to a char*
  21. constsize_t newsize =100;
  22. char nstring[newsize];
  23. strcpy_s(nstring,(char*)orig);
  24. strcat_s(nstring," (char *)");
  25. cout << nstring << endl;
  26.  
  27. // Convert to a wchar_t*
  28. wchar_t wcstring[newsize];
  29. wcscpy_s(wcstring,(wchar_t*)orig);
  30. wcscat_s(wcstring, L" (wchar_t *)");
  31. wcout << wcstring << endl;
  32.  
  33. // Convert to a CComBSTR
  34. CComBSTR ccombstr((char*)orig);
  35. if(ccombstr.Append(L" (CComBSTR)")== S_OK)
  36. {
  37. CW2A printstr(ccombstr);
  38. cout << printstr << endl;
  39. }
  40.  
  41. // Convert to a CString
  42. CString cstring((char*)orig);
  43. cstring +=" (CString)";
  44. cout << cstring << endl;
  45.  
  46. // Convert to a basic_string
  47. string basicstring((char*)orig);
  48. basicstring +=" (basic_string)";
  49. cout << basicstring << endl;
  50.  
  51. // Convert to a System::String
  52. String^systemstring = gcnew String((char*)orig);
  53. systemstring +=" (System::String)";
  54. Console::WriteLine("{0}", systemstring);
  55. delete systemstring;
  56. }

输出

  1. Hello,World!(_bstr_t)
  2. Hello,World!(char*)
  3. Hello,World!(wchar_t*)
  4. Hello,World!(CComBSTR)
  5. Hello,World!(CString)
  6. Hello,World!(basic_string)
  7. Hello,World!(System::String)

从 CComBSTR 转换

示例

说明

此示例演示如何从 CComBSTR 转换为上面列出的其他字符串类型。

  1. // convert_from_ccombstr.cpp
  2. // compile with /clr /link comsuppw.lib
  3.  
  4. #include<iostream>
  5. #include<stdlib.h>
  6. #include<string>
  7.  
  8. #include"atlbase.h"
  9. #include"atlstr.h"
  10. #include"comutil.h"
  11. #include"vcclr.h"
  12.  
  13. usingnamespace std;
  14. usingnamespaceSystem;
  15. usingnamespaceSystem::Runtime::InteropServices;
  16.  
  17. int main()
  18. {
  19. CComBSTR orig("Hello, World!");
  20. CW2A printstr(orig);
  21. cout << printstr <<" (CComBSTR)"<< endl;
  22.  
  23. // Convert to a char*
  24. constsize_t newsize =100;
  25. char nstring[newsize];
  26. CW2A tmpstr1(orig);
  27. strcpy_s(nstring, tmpstr1);
  28. strcat_s(nstring," (char *)");
  29. cout << nstring << endl;
  30.  
  31. // Convert to a wchar_t*
  32. wchar_t wcstring[newsize];
  33. wcscpy_s(wcstring, orig);
  34. wcscat_s(wcstring, L" (wchar_t *)");
  35. wcout << wcstring << endl;
  36.  
  37. // Convert to a _bstr_t
  38. _bstr_t bstrt(orig);
  39. bstrt +=" (_bstr_t)";
  40. cout << bstrt << endl;
  41.  
  42. // Convert to a CString
  43. CString cstring(orig);
  44. cstring +=" (CString)";
  45. cout << cstring << endl;
  46.  
  47. // Convert to a basic_string
  48. wstring basicstring(orig);
  49. basicstring += L" (basic_string)";
  50. wcout << basicstring << endl;
  51.  
  52. // Convert to a System::String
  53. String^systemstring = gcnew String(orig);
  54. systemstring +=" (System::String)";
  55. Console::WriteLine("{0}", systemstring);
  56. delete systemstring;
  57. }

输出

  1. Hello,World!(CComBSTR)
  2. Hello,World!(char*)
  3. Hello,World!(wchar_t*)
  4. Hello,World!(_bstr_t)
  5. Hello,World!(CString)
  6. Hello,World!(basic_string)
  7. Hello,World!(System::String)

从 CString 转换

示例

说明

此示例演示如何从 CString 转换为上面列出的其他字符串类型。

  1. // convert_from_cstring.cpp
  2. // compile with /clr /link comsuppw.lib
  3.  
  4. #include<iostream>
  5. #include<stdlib.h>
  6. #include<string>
  7.  
  8. #include"atlbase.h"
  9. #include"atlstr.h"
  10. #include"comutil.h"
  11.  
  12. usingnamespace std;
  13. usingnamespaceSystem;
  14.  
  15. int main()
  16. {
  17. CString orig("Hello, World!");
  18. wcout << orig <<" (CString)"<< endl;
  19.  
  20. // Convert to a char*
  21. constsize_t newsize =100;
  22. char nstring[newsize];
  23. strcpy_s(nstring, orig);
  24. strcat_s(nstring," (char *)");
  25. cout << nstring << endl;
  26.  
  27. // Convert to a wchar_t*
  28. // You must first convert to a char * for this to work.
  29. size_t origsize = strlen(orig)+1;
  30. size_t convertedChars =0;
  31. wchar_t wcstring[newsize];
  32. mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
  33. wcscat_s(wcstring, L" (wchar_t *)");
  34. wcout << wcstring << endl;
  35.  
  36. // Convert to a _bstr_t
  37. _bstr_t bstrt(orig);
  38. bstrt +=" (_bstr_t)";
  39. cout << bstrt << endl;
  40.  
  41. // Convert to a CComBSTR
  42. CComBSTR ccombstr(orig);
  43. if(ccombstr.Append(L" (CComBSTR)")== S_OK)
  44. {
  45. CW2A printstr(ccombstr);
  46. cout << printstr << endl;
  47. }
  48.  
  49. // Convert to a basic_string
  50. string basicstring(orig);
  51. basicstring +=" (basic_string)";
  52. cout << basicstring << endl;
  53.  
  54. // Convert to a System::String
  55. String^systemstring = gcnew String(orig);
  56. systemstring +=" (System::String)";
  57. Console::WriteLine("{0}", systemstring);
  58. delete systemstring;
  59. }

输出

  1. Hello,World!(CString)
  2. Hello,World!(char*)
  3. Hello,World!(wchar_t*)
  4. Hello,World!(_bstr_t)
  5. Hello,World!(CComBSTR)
  6. Hello,World!(basic_string)
  7. Hello,World!(System::String)

从 basic_string 转换

示例

说明

此示例演示如何从 basic_string 转换为上面列出的其他字符串类型。

  1. // convert_from_basic_string.cpp
  2. // compile with /clr /link comsuppw.lib
  3.  
  4. #include<iostream>
  5. #include<stdlib.h>
  6. #include<string>
  7.  
  8. #include"atlbase.h"
  9. #include"atlstr.h"
  10. #include"comutil.h"
  11.  
  12. usingnamespace std;
  13. usingnamespaceSystem;
  14.  
  15. int main()
  16. {
  17. string orig("Hello, World!");
  18. cout << orig <<" (basic_string)"<< endl;
  19.  
  20. // Convert to a char*
  21. constsize_t newsize =100;
  22. char nstring[newsize];
  23. strcpy_s(nstring, orig.c_str());
  24. strcat_s(nstring," (char *)");
  25. cout << nstring << endl;
  26.  
  27. // Convert to a wchar_t*
  28. // You must first convert to a char * for this to work.
  29. size_t origsize = strlen(orig.c_str())+1;
  30. size_t convertedChars =0;
  31. wchar_t wcstring[newsize];
  32. mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
  33. wcscat_s(wcstring, L" (wchar_t *)");
  34. wcout << wcstring << endl;
  35.  
  36. // Convert to a _bstr_t
  37. _bstr_t bstrt(orig.c_str());
  38. bstrt +=" (_bstr_t)";
  39. cout << bstrt << endl;
  40.  
  41. // Convert to a CComBSTR
  42. CComBSTR ccombstr(orig.c_str());
  43. if(ccombstr.Append(L" (CComBSTR)")== S_OK)
  44. {
  45. CW2A printstr(ccombstr);
  46. cout << printstr << endl;
  47. }
  48.  
  49. // Convert to a CString
  50. CString cstring(orig.c_str());
  51. cstring +=" (CString)";
  52. cout << cstring << endl;
  53.  
  54. // Convert to a System::String
  55. String^systemstring = gcnew String(orig.c_str());
  56. systemstring +=" (System::String)";
  57. Console::WriteLine("{0}", systemstring);
  58. delete systemstring;
  59. }

输出

  1. Hello,World!(basic_string)
  2. Hello,World!(char*)
  3. Hello,World!(wchar_t*)
  4. Hello,World!(_bstr_t)
  5. Hello,World!(CComBSTR)
  6. Hello,World!(CString)
  7. Hello,World!(System::String)

从 System::String 转换

示例

说明

此示例演示如何从 System.String 转换为上面列出的其他字符串类型。

  1. // convert_from_system_string.cpp
  2. // compile with /clr /link comsuppw.lib
  3.  
  4. #include<iostream>
  5. #include<stdlib.h>
  6. #include<string>
  7.  
  8. #include"atlbase.h"
  9. #include"atlstr.h"
  10. #include"comutil.h"
  11. #include"vcclr.h"
  12.  
  13. usingnamespace std;
  14. usingnamespaceSystem;
  15. usingnamespaceSystem::Runtime::InteropServices;
  16.  
  17. int main()
  18. {
  19. String^orig = gcnew String("Hello, World!");
  20. Console::WriteLine("{0} (System::String)", orig);
  21.  
  22. pin_ptr<constwchar_t> wch =PtrToStringChars(orig);
  23.  
  24. // Convert to a char*
  25. size_t origsize = wcslen(wch)+1;
  26. constsize_t newsize =100;
  27. size_t convertedChars =0;
  28. char nstring[newsize];
  29. wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
  30. strcat_s(nstring," (char *)");
  31. cout << nstring << endl;
  32.  
  33. // Convert to a wchar_t*
  34. wchar_t wcstring[newsize];
  35. wcscpy_s(wcstring, wch);
  36. wcscat_s(wcstring, L" (wchar_t *)");
  37. wcout << wcstring << endl;
  38.  
  39. // Convert to a _bstr_t
  40. _bstr_t bstrt(wch);
  41. bstrt +=" (_bstr_t)";
  42. cout << bstrt << endl;
  43.  
  44. // Convert to a CComBSTR
  45. CComBSTR ccombstr(wch);
  46. if(ccombstr.Append(L" (CComBSTR)")== S_OK)
  47. {
  48. CW2A printstr(ccombstr);
  49. cout << printstr << endl;
  50. }
  51.  
  52. // Convert to a CString
  53. CString cstring(wch);
  54. cstring +=" (CString)";
  55. cout << cstring << endl;
  56.  
  57. // Convert to a basic_string
  58. wstring basicstring(wch);
  59. basicstring += L" (basic_string)";
  60. wcout << basicstring << endl;
  61.  
  62. delete orig;
  63. }

输出

  1. Hello,World!(System::String)
  2. Hello,World!(char*)
  3. Hello,World!(wchar_t*)
  4. Hello,World!(_bstr_t)
  5. Hello,World!(CComBSTR)
  6. Hello,World!(CString)
  7. Hello,World!(basic_string)
  1. 转自:http://blog.donews.com/keo321/?p=1583425

转载于:https://www.cnblogs.com/Dennis-mi/articles/3461533.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值