UTF8로 인코딩 혹은 디코딩

char* UTF8_Encode(LPCTSTR str)
{
   USES_CONVERSION;
   /* str이 Unicode인지 Ansi 인지 따질 필요없게 T2CW로 변환 */ 
   const WCHAR* wStr = T2CW(str); 

   /* 길이는 -1로 주어 널NULL 문자도 변환되도록 */ 
   /*WCHAR -> UTF-8 */ 

   /*wStr의 크기를 구함 */ 
   int nUTF8codeSize = WideCharToMultiByte(CP_UTF8, 0, wStr, -1, NULL, 0, NULL, NULL);
   char *utf8Str = new char[nUTF8codeSize]; 

   WideCharToMultiByte(CP_UTF8, 0, wStr, -1, utf8Str, nUTF8codeSize, 0, 0); 

   return utf8Str;
}

CString UTF8_Decode(LPCSTR utf8str)
{
   int size = MultiByteToWideChar(CP_UTF8, 0, utf8str, -1, NULL, 0);
   LPWSTR wStr = new WCHAR[size];
   MultiByteToWideChar(CP_UTF8, 0, utf8str, -1, wStr, size); 

   USES_CONVERSION;
   CString str = W2CT(wStr);
   delete[] wStr;

   return str;
}

Related Posts

답글 남기기

이메일 주소는 공개되지 않습니다.