블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2735)
Unity3D (815)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (58)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (53)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
04-17 00:04

'문자열변환'에 해당되는 글 3건

  1. 2010.05.26 다양한 문자열 형식 간 변환
  2. 2010.05.26 System::String -> char* OR wchar_t*
  3. 2010.05.26 Managed 문자열 -> Unmanaged 문자열 변환
반응형

'Programming > C++/CLI' 카테고리의 다른 글

System::String 관련  (0) 2010.05.26
혼합 모드에서 디버깅  (0) 2010.05.26
C++/CLI & WCF Programming  (1) 2010.05.26
Native C++ & C++ / CLI & C# 환경에서의 디버깅  (0) 2010.05.26
System::String -> char* OR wchar_t*  (0) 2010.05.26
Posted by blueasa
, |

이번절에서는 닷넷 프레임 워크의 System::String를 일반적으로 C++ 에서 사용할 수 있는 char* 또는 wchar_t* 로 변환 해보도록 해봅시다. 간단한 코드라서 전체 소스와 주석으로 설명을 대신 하겠습니다.

#pragma once


#include <vcclr.h>

#include <atlstr.h>

#include <stdio.h>

#using <mscorlib.dll>

using namespace System;

using namespace System::Runtime::InteropServices;


namespace MainEngine {


    public ref class MainRenderEngine

    {

        // TODO: Add your methods for this class here.

    public:

        bool Create(System::String^ strWindowText)

        {

            // 유니코드 문자열에 할당

            pin_ptr<const wchar_t> wch = PtrToStringChars(strWindowText);


            MessageBox(NULL, wch, _T("유니코드"), MB_OK);


            // 일반 char* 문자열로 변환

            size_t convertedChars = 0;

            size_t  sizeInBytes = ((strWindowText->Length + 1) * 2);

            errno_t err = 0;

            char    *ch = (char *)malloc(sizeInBytes);


            err = wcstombs_s(&convertedChars, ch, sizeInBytes, wch, sizeInBytes);


            if (err != 0)

            {

                MessageBox(NULL, _T("char* 변환 에러"), _T("에러"), MB_OK);

            }


            // ch의 사용

            printf_s("%s\n", ch);



            return true;

        }

    };

}



반응형
Posted by blueasa
, |

//-- 인코딩 지정 변환 예

System::Text::Encoding^ m_encoding;

m_encoding = gcnew System::Text::UTF8Encoding();
m_stat, sql::SQLException* : legacy native c++ pointer

int execute(String^ sql) 
try {
assert(m_stat); 
if (!m_encoding) {
return m_stat->execute(getAnsiString(sql)); //.net -> native
} else {

array<unsigned char>^ bytes = m_encoding->GetBytes(sql);
pin_ptr<unsigned char> s = &bytes[0];
return m_stat->execute((const char*)s);   //.net -> native
}
} catch (sql::SQLException *ex) {
throw gcnew SQLException(ex, sql);
}
}

//-- utf8 로 저장된 문자열 배열을, .net 유니코드 문자열 배열로 변환
array<String^>^ convStrArray(const std::vector<std::string> &arrStr) //native -> .net
{
size_t cnt = arrStr.size();
UTF8Encoding^ encode = gcnew UTF8Encoding(true,true);
        array<String^>^ arr = gcnew array<String^>(cnt);
        for (size_t i = 0; i < cnt; i++)
            arr[i] = gcnew String(arrStr[i].c_str(), 0, arrStr[i].size(), encode);

        return arr;
}


//-- strconv.h, vs2005/8

#pragma once

#include <vcclr.h>
#include <string>

using namespace System;
using namespace System::Runtime::InteropServices;

inline
std::string getAnsiString(String^ s) 
{
    IntPtr ip = Marshal::StringToHGlobalAnsi(s);
    const char* str = static_cast<const char*>(ip.ToPointer());

    std::string ansi(str);
    
    Marshal::FreeHGlobal( ip );

    return ansi;
}

inline
std::wstring getUniString(String^ s)
{
    pin_ptr<const wchar_t> str = PtrToStringChars(s);

    std::wstring uni(str);
    return uni;
반응형

'Programming > C++/CLI' 카테고리의 다른 글

System::String -> char* OR wchar_t*  (0) 2010.05.26
CLI - 초무식, 초단순 암기용 저장글  (0) 2010.05.26
C++/CLI 예제  (0) 2010.05.25
참고 사이트  (0) 2010.05.25
CLI의 기본 타입  (0) 2010.05.25
Posted by blueasa
, |