2010년 4월 5일 월요일

인코더, 디코더

#include <atlconv.h>

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

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

    int nUTF8codeSize = WideCharToMultiByte(CP_UTF8, 0, wStr, -1, NULL, 0, NULL, NULL); //wStr의 크기를 구함
    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;
}

댓글 없음:

댓글 쓰기