zoukankan      html  css  js  c++  java
  • Visual C++ 如何:在各种字符串类型之间进行转换

    本主题演示如何将各种 C++ 字符串类型转换为其他字符串。可以转换的字符串类型包括 char *、wchar_t*、_bstr_tCComBSTRCStringbasic_stringSystem.String。在所有情况下,在将字符串转换为新类型时,都会创建字符串的副本。对新字符串进行的任何更改都不会影响原始字符串,反之亦然。

    从 char * 转换

    // convert_from_char.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()
    {
    char *orig = "Hello, World!";
    cout << orig << " (char *)" << endl;
    // Convert to a wchar_t*
    size_t origsize = strlen(orig) + 1;
    const size_t newsize = 100;
    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 CString
    CString cstring(orig);
    cstring += " (CString)";
    cout << cstring << 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;
    }
    

    从 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;
    }
    

    从 _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;
    }
    

     

    从 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;
    }
    

    关于函数:wcstombs_s, _wcstombs_s_l 

    函数描述:Converts a sequence of wide characters to a corresponding sequence of multibyte characters.

    errno_t wcstombs_s(
       size_t *pReturnValue,
       char *mbstr,
       size_t sizeInBytes,
       const wchar_t *wcstr,
       size_t count 
    );
    errno_t _wcstombs_s_l(
       size_t *pReturnValue,
       char *mbstr,
       size_t sizeInBytes,
       const wchar_t *wcstr,
       size_t count,
       _locale_t locale
    );
    

     

    Parameters

    [out] pReturnValue
    
    The number of characters converted.
    
    [out] mbstr
    
    The address of a buffer for the resulting converted multibyte character string.
    
    [in] sizeInBytes
    
    The size in bytes of the mbstr buffer.
    
    [in] wcstr
    
    Points to the wide character string to be converted.
    
    [in] count
    
    The maximum number of bytes to be stored in the mbstr buffer, or _TRUNCATE.
    
    [in] locale
    
    The locale to use.
    
  • 相关阅读:
    几款免费的支持HTML5的音频视频转换软件推荐
    2 宽度优先爬虫和带偏好的爬虫(4)
    Hadoop源代码分析(三)
    Hadoop源代码分析(四)
    C# 收邮件
    关于Adobe flash palyer 安装出现的问题解决方案
    C#调用java类、jar包方法。
    EF 4.3 的一些基础使用
    .net数据库连接池问题:在同一页面使用一段时间后,提示超时,连接池不够用这类的提示!
    使用Google CDN的JSAPI服务来提供加载各类JS库的方法
  • 原文地址:https://www.cnblogs.com/lilun/p/1669463.html
Copyright © 2011-2022 走看看