2012년 5월 18일 금요일

ANSI <-> UTF-8 <-> Unicode 변환


int UnicodetoUTF8( LPWSTR pUniStr, int iLength, LPBYTE pUtf8Str )
{
WCHAR wChar;
BYTE szBytes[4] = { 0 };
int nbytes;
int i, j;
int iTotelLen = 0;

for( i = 0; i < iLength; i++ )
{
wChar = pUniStr[i];

if( 0x80 > wChar )
{
nbytes = 1;
szBytes[0] = (BYTE)wChar;
}
else if( 0x800 > wChar )
{
nbytes = 2;
szBytes[1] = ( wChar & 0x3f ) | 0x80;
szBytes[0] = ( ( wChar << 2 ) & 0xcf00 | 0xc000) >> 8;
}
else
{
nbytes = 3;
szBytes[2] = ( wChar & 0x3f ) | 0x80;
szBytes[1] = ( ( wChar << 2 ) & 0x3f00 | 0x8000) >> 8;
szBytes[0] = ( ( wChar << 4 ) & 0x3f0000 | 0xe00000) >> 16;
}

for( j = 0; j < nbytes; j++ )
{
pUtf8Str[iTotelLen] = szBytes[j];
iTotelLen++;
}
}

pUtf8Str[iTotelLen] = '\0';

return iTotelLen;
}

int Utf8ToUnicode( LPBYTE pUtf8Str, LPWSTR pUniStr )
{
int iIndex = 0;
int iCount = 0;
WCHAR wChar;

while( 0 != pUtf8Str[iIndex] )
{
if( ( 0xE0 == ( pUtf8Str[iIndex] & 0xE0 ) ) )
{
wChar = ( ( pUtf8Str[iIndex] & 0x0f ) << 12 ) |
( ( pUtf8Str[iIndex+1]&0x3F ) << 6 ) |
( pUtf8Str[iIndex+2] & 0x3F );

iIndex += 3;
}
else if( 0xC0 == ( pUtf8Str[iIndex] & 0xC0 ) )
{
wChar = ( ( pUtf8Str[iIndex] & 0x1F ) << 6 ) |
( pUtf8Str[iIndex+1] & 0x3F );

iIndex += 2;
}
else
{
wChar = pUtf8Str[iIndex] & 0x7F;

iIndex++;
}

pUniStr[iCount] = wChar;

iCount++;
}

pUniStr[iCount] = 0;

return iCount;
}

//char* UTF8ToANSI(char *pszCode)
int UTF8ToANSI(char *pszCode, char *pszAnsi)
{
BSTR    bstrWide;
//char*   pszAnsi;
int     nLength;

nLength = MultiByteToWideChar(CP_UTF8, 0, pszCode, lstrlen(pszCode) + 1, NULL, NULL);
bstrWide = SysAllocStringLen(NULL, nLength);

MultiByteToWideChar(CP_UTF8, 0, pszCode, lstrlen(pszCode) + 1, bstrWide, nLength);

nLength = WideCharToMultiByte(CP_ACP, 0, bstrWide, -1, NULL, 0, NULL, NULL);
//pszAnsi = new char[nLength];

WideCharToMultiByte(CP_ACP, 0, bstrWide, -1, pszAnsi, nLength, NULL, NULL);
SysFreeString(bstrWide);
//return pszAnsi;
return nLength;
}


char * ANSIToUTF8(char * pszCode)
{
int nLength, nLength2;
BSTR bstrCode;
char *pszUTFCode = NULL;

nLength = MultiByteToWideChar(CP_ACP, 0, pszCode, lstrlen(pszCode), NULL, NULL);
bstrCode = SysAllocStringLen(NULL, nLength);
MultiByteToWideChar(CP_ACP, 0, pszCode, lstrlen(pszCode), bstrCode, nLength);


nLength2 = WideCharToMultiByte(CP_UTF8, 0, bstrCode, -1, pszUTFCode, 0, NULL, NULL);
pszUTFCode = (char*)malloc(nLength2+1);
WideCharToMultiByte(CP_UTF8, 0, bstrCode, -1, pszUTFCode, nLength2, NULL, NULL);

return pszUTFCode;
}

< 출처 - http://blog.kangsoo.com/14http://skorea.tistory.com/43 >

댓글 없음:

댓글 쓰기