Native/C++
멀티바이트를 유니코드 문자열로 혹은 역으로 변환하는 함수
aucd29
2013. 10. 2. 19:03
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]
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]