2006-07-31

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

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

输出

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 转换

示例

说明

此示例演示如何从
共5页: 上一页 [1] [2] 3 [4] [5] 下一页

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

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