문자열을 인코딩 하는 방법은 여러가지가 있습니다..
가장 널리 알려진 방법으로는
유니코드 -> 멀티바이트
wchar_t strUnicode[256] = {0,};
char strMultibyte[256] = {0,};
wcscpy_s(strUnicode,256,L"유니코드");
int len = WideCharToMultiByte( CP_ACP, 0, strUnicode, -1, NULL, 0, NULL, NULL );
WideCharToMultiByte( CP_ACP, 0, strUnicode, -1, strMultibyte, len, NULL, NULL );
stl이용wstring strUni = L"유니코드"; int len = WideCharToMultiByte( CP_ACP, 0, &strUni[0], -1, NULL, 0, NULL, NULL ); string strMulti(len,0); WideCharToMultiByte( CP_ACP, 0, &strUni[0], -1, &strMulti[0], len, NULL, NULL );
멀티바이트 -> 유니코드
wchar_t strUnicode[256] = {0,};
char strMultibyte[256] = {0,};
strcpy_s(strMultibyte,256,"멀티바이트");
int nLen = MultiByteToWideChar(CP_ACP, 0, strMultibyte, strlen(strMultibyte), NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, strMultibyte, strlen(strMultibyte), strUnicode, nLen);
stl이용 string strMulti = "멀티바이트"; int nLen = MultiByteToWideChar(CP_ACP, 0, &strMulti[0], strMulti.size(), NULL, NULL); wstring strUni(nLen,0); MultiByteToWideChar(CP_ACP, 0, &strMulti[0], strMulti.size(), &strUni[0], nLen);
유니코드 -> UTF-8
wchar_t strUni[256] =L"유니코드";
char strUtf8[256] ={0,};
int nLen = WideCharToMultiByte(CP_UTF8, 0, strUni, lstrlenW(strUni), NULL, 0, NULL, NULL);
WideCharToMultiByte (CP_UTF8, 0, strUni, lstrlenW(strUni), strUtf8, nLen, NULL, NULL);
UTF-8 -> 유니코드로 변환
wchar_t strUnicode[256] = {0,};
char strUTF8[256] = {0,};
strcpy_s(strUTF8,256,"utf-8글자..");// 이건 사실 멀티바이트지만 UTF8이라고 생각해주세요 -_-;;
int nLen = MultiByteToWideChar(CP_UTF8, 0, strUTF8, strlen(strUTF8), NULL, NULL);
MultiByteToWideChar(CP_UTF8, 0, strUTF8, strlen(strUTF8), strUnicode, nLen);
기본적으로 UTF-8로 변형할땐 유니코드 상태에서만 변형을 시켜야 된다!.
| 만약 멀티 바이트를 UTF-8로 변형하고 싶을때에는 멀티바이트 -> 유니코드(UTF-16) -> UTF-8 UTF-8을 멀티바이트로 변형할때에는 UTF-8 -> 유니코드(UTF-16) -> 멀티바이트.. |
그런데 위와 같은 방식은.. 윈도우 환경에서만 사용한다면 너무 복잡하다...
우리 위대하신 MS에서 만들어주신게 있는데..
#include <atlstr.h> // 요기에 정의.. 이거하면 MFC사용안하고도 CString를 사용할수 있다
void main()
{
wstring strUni = CA2W("멀티바이트를 유니코드로 변환");
string strMulti = CW2A(L"유니코드를 멀티바이트로 변환");
string strUTF8 = CW2A(L"유니코드를 UTF8로변환",CP_UTF8);
//string에서 포인터 얻어오는게 c_str()이듯.
//CA2W나 CW2A에서 포인터 얻어오는건 m_psz 이다..
//그리고 CA2W CW2A는 기본적으로 CString 즉 (CAtlString)에 기반을 두고 고 있기때문에.
//CString를 사용할때 가장 빠른다!!.
// 만약 멀티 플레폼을 기준으로 한다면 CA2W는 사용 못함!
}
사용하기도 쉽고 속도면에서 MultiByteToWideChar,WideCharToMultiByte 보다 빠르다...
'C..C++....' 카테고리의 다른 글
| 문자열 사용.. (0) | 2011.07.06 |
|---|---|
| 람다(Lambda) 활용하기.! (0) | 2011.06.27 |
| error LNK 2005 xxxx이(가) MSVCRT(D).lib 혹은 LIBCMT(D).lib 에 이미 정의되어 있습니다.. 오류날때 대처방법 (2) | 2011.06.13 |
| gtest,unittest++ (0) | 2011.05.19 |
| std::tr1::bind / function 사용방법 (0) | 2011.05.18 |