관리 메뉴

HeBhy, since 1983.

VC++ UTF-8 decode into Unicode 본문

Dev/Dev basics

VC++ UTF-8 decode into Unicode

HeBhy 2008. 8. 7. 06:09
//Visual C++
#include <atlconv.h> // For USES_CONVERSION

CString DecodetoUtf8(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;
}

Encode
 1. 사용하는 문자셋(T) -> wchar(Unicode)
 2. Unicode -> UTF8

Decode
 1. UTF8 -> wchar(Unicode)
 2. Unicode -> 사용하는 문자셋(T)






 FILE* fp;
 
 if( (fp = fopen("c:\\rss.xml", "r")) == NULL)
 {
  AfxMessageBox("핸들링 오류");
  PostQuitMessage(-1);
 }

// TCHAR* tmp = new TCHAR[256];

 char tmp[512];

 while(fgets(tmp, sizeof(tmp), fp) != NULL) {
  strcat(buf, tmp);
/************** test***********************/
// tmpstr.Format("tmp = %s", tmp);
// AfxMessageBox(tmpstr);   // -- debug
/*************************************/
 }
 fclose(fp);

/*************** test**********************/
 tmpstr.Format("buf = %s", buf);

 TCHAR* des;

 //UTP8 -> ANSI_CHAR
 if(tmpstr.Find("UTF-8") > 30) // UTF-8 Documents
 {
  // ::API Method Calling
  tmpstr = DecodetoUtf8(tmpstr); //Return CString

  // Convert CString into TChar *
  des = new TCHAR[tmpstr.GetLength()+ 1];
  _tcscpy(des, tmpstr.GetBuffer(0));
  buf = des;
 
  str.Format("%s", buf);
  str.Replace("\n", "\r\n");

  // depend on XML Structure
  _parseString->GetItemTitles(buf, strSize, m_li, CODEORDIE);
 }
 else       // For other Documents for now
 {
  str.Format("%s", buf);
  str.Replace("\n", "\r\n");
  _parseString->GetItemTitles(buf, strSize, m_li, _feedType);
 }

Comments