2006-07-31

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

来源: CSDN 作者:guanchanghui 评论 0 条
 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;

}


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

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