简洁的想法

 找回密码
 注册
搜索
查看: 738|回复: 3

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

[复制链接]
发表于 2012-6-19 13:07:45 | 显示全部楼层 |阅读模式
本主题演示如何将各种 Visual C++ 字符串类型转换为其他字符串。可以转换的字符串类型包括 char *、wchar_t*、_bstr_tCComBSTRCStringbasic_stringSystem.String 。在所有情况下,在将字符串转换为新类型时,都会创建字符串的副本。对新字符串进行的任何更改都不会影响原始字符串,反之亦然。


从 char * 转换
示例
此示例演示如何从 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. }
复制代码
输出
  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)
复制代码
转自:http://msdn.microsoft.com/zh-cn/library/ms235631(v=vs.80).aspx
 楼主| 发表于 2012-6-19 13:08:39 | 显示全部楼层
从 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)
 楼主| 发表于 2012-7-20 10:57:49 | 显示全部楼层
也谈C++中char*与wchar_t*之间的转换

关于C++中的char*与wchar_t*这两种类型的相互转换,网上说的大多很繁琐,可行性也不高。下面这个方法是在MSDN里面找到的,个人认为还比较不错:

把char*转换为wchar_t*
用stdlib.h中的mbstowcs_s函数,可以通过下面的例子了解其用法:
  1. char *CStr = "string to convert";
  2. size_t len = strlen(CStr) + 1;
  3. size_t converted = 0;
  4. wchar_t *WStr;
  5. WStr=(wchar_t*)malloc(len*sizeof(wchar_t));
  6. mbstowcs_s(&converted, WStr, len, CStr, _TRUNCATE);
复制代码
其结果是WStr中储存了CStr的wchar_t版本。

把wchar_t*转换为char*
和上面的方法类似,用stdlib.h中的wcstombs_s函数,例子:
  1. wchar_t *WStr = L"string to convert";
  2. size_t len = wcslen(WStr) + 1;
  3. size_t converted = 0;
  4. char *CStr;
  5. CStr=(char*)malloc(len*sizeof(char));
  6. wcstombs_s(&converted, CStr, len, WStr, _TRUNCATE);
复制代码
这时WStr中的内容将被转化为char版本储存在CStr中。

另外还可以通过流的方法来char*类型转换为wchar_t*类型,但这样的转换得到的结果将是const类型,而类似的方法不能将wchar_t*类型转换为char*类型。

把(const)char*转换为const wchar_t*
需要用到 sstream 头文件:
  1. char *cstr="string to convert";
  2. wstringstream wss;
  3. wss<<cstr;
复制代码
再调用wss.str().c_str(); 即可得到 const wchar_t* 类型的返回值。

虽然stringstream流不能将wchar_t*转换成char*,但可以用来进行数值类型和字符串之间的转换,例如:
  1. double d=2734792.934f;
  2. stringstream ss;
  3. ss<<d;
复制代码
调用ss.str()可得到string类型字符串 ”273479e+006”,又如:
  1. string str("299792458");
  2. stringstream ss;
  3. long i=0;
  4. ss<<str;
  5. ss>>i;
复制代码
此时i=299792458。


转自: http://blog.163.com/tianshi_17th ... 418920085209414977/
 楼主| 发表于 2012-7-20 10:59:28 | 显示全部楼层
  1. #include <windows.h>
  2. #include <stdio.h>

  3. //function: charTowchar
  4. //purpose:char to WCHAR 、wchar_t、LPWSTR etc
  5. void charTowchar(const char *chr, wchar_t *wchar, int size)
  6. {   
  7.         MultiByteToWideChar( CP_ACP, 0, chr,
  8.         strlen(chr)+1, wchar, size/sizeof(wchar[0]) );
  9. }

  10. //function: wcharTochar
  11. //purpose:WCHAR 、wchar_t、LPWSTR to char
  12. void wcharTochar(const wchar_t *wchar, char *chr, int length)
  13. {
  14.     WideCharToMultiByte( CP_ACP, 0, wchar, -1,
  15.         chr, length, NULL, NULL );
  16. }

  17. int main (void)
  18. {
  19.         char     chr[128];
  20.         wchar_t  *wchar = L"陈鸿钦";
  21.        

  22.         //wchar_t to char
  23.         wcharTochar(wchar, chr, sizeof(chr));
  24.         printf("char is %s\n", chr);

  25.         //char to wchar_t
  26.         wchar = (wchar_t *)malloc(sizeof(wchar_t) * 64);
  27.         charTowchar(chr, wchar, sizeof(wchar_t) * 64);
  28.        
  29.         wprintf_s(L"%s\n", wchar);//
  30.         getchar();

  31.         return 0;
  32. }
复制代码
转自: http://futrueboy.iteye.com/blog/240465
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|Archiver|简洁的想法

GMT+1, 2022-1-28 18:01

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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