본문 바로가기

Native/C++

멀티바이트를 유니코드 문자열로 혹은 역으로 변환하는 함수

mbstowcs() : 멀티바이트 스트링을 와이드캐릭터 스트링으로 변경
wcstombs() : 와이드캐릭터 스트링을 멀티바이트 스트링으로 변경


[code]
#ifdef WIN32
#pragma warning(disable:4786)
#endif

#include <iostream>

using namespace std;

void string2wstring(wstring &dest,const string &src)
{
     dest.resize(src.size());
     for ( unsigned int i=0; i<src.size(); i++)
        dest[i] = static_cast<unsigned char>(src[i]);
}

void wstring2string(string &dest,const wstring &src)
{
     dest.resize(src.size());
     for (unsigned int i=0; i<src.size(); i++)
         dest[i] = src[i] < 256 ? src[i] : ' ';
}

int main ( int argc, char** argv )
{
string str = "한글 English";
wstring wstr;

string2wstring( wstr, str );
wcout << L"[2Byte] " << wstr.c_str() << endl;
wstring2string ( str, wstr );
cout << "[1Byte] " << str.c_str() << endl;
return 0;
}
[/code]

'Native > C++' 카테고리의 다른 글

singletone  (0) 2013.10.02
virtual int memberFunc() = 0; 에서 = 0 에 의미  (0) 2013.10.02
L 리터럴과 유니코드 인코딩 (#pragma setlocale(  (0) 2013.10.02
reverse_iterator 를 iterator로...  (0) 2013.10.02
cos 세타 구하기  (0) 2013.10.02