ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C++의 다양한 문자열 변환
    개발/지름 2007. 11. 13. 00:52
    반응형
    GDI+의 문자열 사용에 치를 떨다가 찾아냈다.
    놀랍게도 -_-...... msnd온라인에 있었다. (못찾은 내가 바보인가....-_-)
    유니코드로 프로그래밍 할때 가장 난적인 캐릭터 변환이다.
    앞으로도 잘 쓸 것 같다.



    Visual C++
    방법: 다양한 문자열 형식 간 변환

    이 항목에서는 다양한 Visual C++ 문자열 형식을 다른 문자열로 변환하는 방법을 보여 줍니다. 여기에서는 char *, wchar_t*, _bstr_t, CComBSTR, CString, basic_stringSystem.String과 같은 문자열 형식을 다룹니다. 모든 경우에서 새 형식으로 변환할 때 문자열의 복사본이 만들어집니다. 새 문자열을 변경해도 원래 문자열에는 영향을 미치지 않으며, 그 반대도 마찬가지입니다.

    char *에서 변환

    예제

    이 예제에서는 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;
    }

    출력

    Hello, World! (char *)
    Hello, World! (wchar_t *)
    Hello, World! (_bstr_t)
    Hello, World! (CComBSTR)
    Hello, World! (CString)
    Hello, World! (basic_string)
    Hello, World! (System::String)

    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)
    반응형

    댓글

Designed by Tistory.