2006-07-31

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

来源: CSDN 作者:guanchanghui 评论 0 条
 

此示例演示如何从 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;

}


共5页: 上一页 [1] 2 [3] [4] [5] 下一页
(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:教你用Myisamchk进行崩溃恢复MySQL  
下一篇:MySQL加密函数保护Web网站敏感数据
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号