鱼C论坛

 找回密码
 立即注册
查看: 2698|回复: 3

[技术交流] MSDN找到的:在各种字符串类型之间进行转换

[复制链接]
发表于 2012-10-4 12:28:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在各种字符串类型之间进行转换
1、从 char * 转换
  1. // convert_from_char.cpp
  2. // compile with: /clr /link comsuppw.lib

  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include <string>

  6. #include "atlbase.h"
  7. #include "atlstr.h"
  8. #include "comutil.h"

  9. using namespace std;
  10. using namespace System;

  11. int main()
  12. {
  13.     char *orig = "Hello, World!";
  14.     cout << orig << " (char *)" << endl;

  15.     // Convert to a wchar_t*
  16.     size_t origsize = strlen(orig) + 1;
  17.     const size_t newsize = 100;
  18.     size_t convertedChars = 0;
  19.     wchar_t wcstring[newsize];
  20.     mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
  21.     wcscat_s(wcstring, L" (wchar_t *)");
  22.     wcout << wcstring << endl;

  23.     // Convert to a _bstr_t
  24.     _bstr_t bstrt(orig);
  25.     bstrt += " (_bstr_t)";
  26.     cout << bstrt << endl;

  27.     // Convert to a CComBSTR
  28.     CComBSTR ccombstr(orig);
  29.     if (ccombstr.Append(L" (CComBSTR)") == S_OK)
  30.     {
  31.         CW2A printstr(ccombstr);
  32.         cout << printstr << endl;
  33.     }

  34.     // Convert to a CString
  35.     CString cstring(orig);
  36.     cstring += " (CString)";
  37.     cout << cstring << endl;

  38.     // Convert to a basic_string
  39.     string basicstring(orig);
  40.     basicstring += " (basic_string)";
  41.     cout << basicstring << endl;

  42.     // Convert to a System::String
  43.     String ^systemstring = gcnew String(orig);
  44.     systemstring += " (System::String)";
  45.     Console::WriteLine("{0}", systemstring);
  46.     delete systemstring;
  47. }
复制代码
输出 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)2、从 wchar_t * 转换
  1. // convert_from_wchar_t.cpp
  2. // compile with: /clr /link comsuppw.lib

  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include <string>

  6. #include "atlbase.h"
  7. #include "atlstr.h"
  8. #include "comutil.h"

  9. using namespace std;
  10. using namespace System;

  11. int main()
  12. {
  13.     wchar_t *orig = L"Hello, World!";
  14.     wcout << orig << L" (wchar_t *)" << endl;

  15.     // Convert to a char*
  16.     size_t origsize = wcslen(orig) + 1;
  17.     const size_t newsize = 100;
  18.     size_t convertedChars = 0;
  19.     char nstring[newsize];
  20.     wcstombs_s(&convertedChars, nstring, origsize, orig, _TRUNCATE);
  21.     strcat_s(nstring, " (char *)");
  22.     cout << nstring << endl;

  23.     // Convert to a _bstr_t
  24.     _bstr_t bstrt(orig);
  25.     bstrt += " (_bstr_t)";
  26.     cout << bstrt << endl;

  27.     // Convert to a CComBSTR
  28.     CComBSTR ccombstr(orig);
  29.     if (ccombstr.Append(L" (CComBSTR)") == S_OK)
  30.     {
  31.         CW2A printstr(ccombstr);
  32.         cout << printstr << endl;
  33.     }

  34.     // Convert to a CString
  35.     CString cstring(orig);
  36.     cstring += " (CString)";
  37.     cout << cstring << endl;

  38.     // Convert to a basic_string
  39.     wstring basicstring(orig);
  40.     basicstring += L" (basic_string)";
  41.     wcout << basicstring << endl;

  42.     // Convert to a System::String
  43.     String ^systemstring = gcnew String(orig);
  44.     systemstring += " (System::String)";
  45.     Console::WriteLine("{0}", systemstring);
  46.     delete systemstring;
  47. }
复制代码

楼下续接

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-10-4 12:31:31 | 显示全部楼层
输出 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)
3、从 _bstr_t 转换
  1. // convert_from_bstr_t.cpp
  2. // compile with: /clr /link comsuppw.lib

  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include <string>

  6. #include "atlbase.h"
  7. #include "atlstr.h"
  8. #include "comutil.h"

  9. using namespace std;
  10. using namespace System;

  11. int main()
  12. {
  13.     _bstr_t orig("Hello, World!");
  14.     wcout << orig << " (_bstr_t)" << endl;

  15.     // Convert to a char*
  16.     const size_t newsize = 100;
  17.     char nstring[newsize];
  18.     strcpy_s(nstring, (char *)orig);
  19.     strcat_s(nstring, " (char *)");
  20.     cout << nstring << endl;

  21.     // Convert to a wchar_t*
  22.     wchar_t wcstring[newsize];
  23.     wcscpy_s(wcstring, (wchar_t *)orig);
  24.     wcscat_s(wcstring, L" (wchar_t *)");
  25.     wcout << wcstring << endl;

  26.     // Convert to a CComBSTR
  27.     CComBSTR ccombstr((char *)orig);
  28.     if (ccombstr.Append(L" (CComBSTR)") == S_OK)
  29.     {
  30.         CW2A printstr(ccombstr);
  31.         cout << printstr << endl;
  32.     }

  33.     // Convert to a CString
  34.     CString cstring((char *)orig);
  35.     cstring += " (CString)";
  36.     cout << cstring << endl;

  37.     // Convert to a basic_string
  38.     string basicstring((char *)orig);
  39.     basicstring += " (basic_string)";
  40.     cout << basicstring << endl;

  41.     // Convert to a System::String
  42.     String ^systemstring = gcnew String((char *)orig);
  43.     systemstring += " (System::String)";
  44.     Console::WriteLine("{0}", systemstring);
  45.     delete systemstring;
  46. }
复制代码
输出 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
4、从 CComBSTR 转换
  1. // convert_from_ccombstr.cpp
  2. // compile with: /clr /link comsuppw.lib

  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include <string>

  6. #include "atlbase.h"
  7. #include "atlstr.h"
  8. #include "comutil.h"
  9. #include "vcclr.h"

  10. using namespace std;
  11. using namespace System;
  12. using namespace System::Runtime::InteropServices;

  13. int main()
  14. {
  15.     CComBSTR orig("Hello, World!");
  16.     CW2A printstr(orig);
  17.     cout << printstr << " (CComBSTR)" << endl;

  18.     // Convert to a char*
  19.     const size_t newsize = 100;
  20.     char nstring[newsize];
  21.     CW2A tmpstr1(orig);
  22.     strcpy_s(nstring, tmpstr1);
  23.     strcat_s(nstring, " (char *)");
  24.     cout << nstring << endl;

  25.     // Convert to a wchar_t*
  26.     wchar_t wcstring[newsize];
  27.     wcscpy_s(wcstring, orig);
  28.     wcscat_s(wcstring, L" (wchar_t *)");
  29.     wcout << wcstring << endl;

  30.     // Convert to a _bstr_t
  31.     _bstr_t bstrt(orig);
  32.     bstrt += " (_bstr_t)";
  33.     cout << bstrt << endl;

  34.     // Convert to a CString
  35.     CString cstring(orig);
  36.     cstring += " (CString)";
  37.     cout << cstring << endl;

  38.     // Convert to a basic_string
  39.     wstring basicstring(orig);
  40.     basicstring += L" (basic_string)";
  41.     wcout << basicstring << endl;

  42.     // Convert to a System::String
  43.     String ^systemstring = gcnew String(orig);
  44.     systemstring += " (System::String)";
  45.     Console::WriteLine("{0}", systemstring);
  46.     delete systemstring;
  47. }
复制代码
输出 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)





补充内容 (2012-10-4 12:32):
楼下继续、、、、未结束,勿回帖
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-10-4 12:38:47 | 显示全部楼层

5、从 CString 转换
  1. // convert_from_cstring.cpp
  2. // compile with: /clr /link comsuppw.lib

  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include <string>

  6. #include "atlbase.h"
  7. #include "atlstr.h"
  8. #include "comutil.h"

  9. using namespace std;
  10. using namespace System;

  11. int main()
  12. {
  13.     CString orig("Hello, World!");
  14.     wcout << orig << " (CString)" << endl;

  15.     // Convert to a char*
  16.     const size_t newsize = 100;
  17.     char nstring[newsize];
  18.     strcpy_s(nstring, orig);
  19.     strcat_s(nstring, " (char *)");
  20.     cout << nstring << endl;

  21.     // Convert to a wchar_t*
  22.     // You must first convert to a char * for this to work.
  23.     size_t origsize = strlen(orig) + 1;
  24.     size_t convertedChars = 0;
  25.     wchar_t wcstring[newsize];
  26.     mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
  27.     wcscat_s(wcstring, L" (wchar_t *)");
  28.     wcout << wcstring << endl;

  29.     // Convert to a _bstr_t
  30.     _bstr_t bstrt(orig);
  31.     bstrt += " (_bstr_t)";
  32.     cout << bstrt << endl;

  33.     // Convert to a CComBSTR
  34.     CComBSTR ccombstr(orig);
  35.     if (ccombstr.Append(L" (CComBSTR)") == S_OK)
  36.     {
  37.         CW2A printstr(ccombstr);
  38.         cout << printstr << endl;
  39.     }

  40.     // Convert to a basic_string
  41.     string basicstring(orig);
  42.     basicstring += " (basic_string)";
  43.     cout << basicstring << endl;

  44.     // Convert to a System::String
  45.     String ^systemstring = gcnew String(orig);
  46.     systemstring += " (System::String)";
  47.     Console::WriteLine("{0}", systemstring);
  48.     delete systemstring;
  49. }
复制代码

输出 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)
6、从 basic_string 转换
  1. // convert_from_basic_string.cpp
  2. // compile with: /clr /link comsuppw.lib

  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include <string>

  6. #include "atlbase.h"
  7. #include "atlstr.h"
  8. #include "comutil.h"

  9. using namespace std;
  10. using namespace System;

  11. int main()
  12. {
  13.     string orig("Hello, World!");
  14.     cout << orig << " (basic_string)" << endl;

  15.     // Convert to a char*
  16.     const size_t newsize = 100;
  17.     char nstring[newsize];
  18.     strcpy_s(nstring, orig.c_str());
  19.     strcat_s(nstring, " (char *)");
  20.     cout << nstring << endl;

  21.     // Convert to a wchar_t*
  22.     // You must first convert to a char * for this to work.
  23.     size_t origsize = strlen(orig.c_str()) + 1;
  24.     size_t convertedChars = 0;
  25.     wchar_t wcstring[newsize];
  26.     mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
  27.     wcscat_s(wcstring, L" (wchar_t *)");
  28.     wcout << wcstring << endl;

  29.     // Convert to a _bstr_t
  30.     _bstr_t bstrt(orig.c_str());
  31.     bstrt += " (_bstr_t)";
  32.     cout << bstrt << endl;

  33.     // Convert to a CComBSTR
  34.     CComBSTR ccombstr(orig.c_str());
  35.     if (ccombstr.Append(L" (CComBSTR)") == S_OK)
  36.     {
  37.         CW2A printstr(ccombstr);
  38.         cout << printstr << endl;
  39.     }

  40.     // Convert to a CString
  41.     CString cstring(orig.c_str());
  42.     cstring += " (CString)";
  43.     cout << cstring << endl;

  44.     // Convert to a System::String
  45.     String ^systemstring = gcnew String(orig.c_str());
  46.     systemstring += " (System::String)";
  47.     Console::WriteLine("{0}", systemstring);
  48.     delete systemstring;
  49. }
复制代码
输出 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)
7、从 System::String 转换
  1. // convert_from_system_string.cpp
  2. // compile with: /clr /link comsuppw.lib

  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include <string>

  6. #include "atlbase.h"
  7. #include "atlstr.h"
  8. #include "comutil.h"
  9. #include "vcclr.h"

  10. using namespace std;
  11. using namespace System;
  12. using namespace System::Runtime::InteropServices;

  13. int main()
  14. {
  15.     String ^orig = gcnew String("Hello, World!");
  16.     Console::WriteLine("{0} (System::String)", orig);

  17.     pin_ptr<const wchar_t> wch = PtrToStringChars(orig);

  18.     // Convert to a char*
  19.     size_t origsize = wcslen(wch) + 1;
  20.     const size_t newsize = 100;
  21.     size_t convertedChars = 0;
  22.     char nstring[newsize];
  23.     wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
  24.     strcat_s(nstring, " (char *)");
  25.     cout << nstring << endl;

  26.     // Convert to a wchar_t*
  27.     wchar_t wcstring[newsize];
  28.     wcscpy_s(wcstring, wch);
  29.     wcscat_s(wcstring, L" (wchar_t *)");
  30.     wcout << wcstring << endl;

  31.     // Convert to a _bstr_t
  32.     _bstr_t bstrt(wch);
  33.     bstrt += " (_bstr_t)";
  34.     cout << bstrt << endl;

  35.     // Convert to a CComBSTR
  36.     CComBSTR ccombstr(wch);
  37.     if (ccombstr.Append(L" (CComBSTR)") == S_OK)
  38.     {
  39.         CW2A printstr(ccombstr);
  40.         cout << printstr << endl;
  41.     }

  42.     // Convert to a CString
  43.     CString cstring(wch);
  44.     cstring += " (CString)";
  45.     cout << cstring << endl;

  46.     // Convert to a basic_string
  47.     wstring basicstring(wch);
  48.     basicstring += L" (basic_string)";
  49.     wcout << basicstring << endl;

  50.     delete orig;
  51. }
复制代码
输出 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)完结!!OK了!



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2013-7-2 11:57:15 | 显示全部楼层
看看老帖,支持下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 18:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表