출처 : 인터넷
[utf8.h]
#ifndef UTF8_H
#define UTF8_H
int MultibyteToUTF8(const char* src,int nbyte,char** dest);
int UNICODE_encode(const char* src,int bytelen,unsigned short** unicode);
int UTF8_encode(const unsigned short* src,int srclen,char** multibyte);
int UTF8ToMultibyte(const char* src,int nbyte,char** dest);
int UNICODE_decode(const unsigned short* src,int srclen,char** multibyte);
int UTF8_decode(const char* src,int bytelen,unsigned short** unicode);
#endif
[utf8.cpp]
int MultibyteToUTF8(const char* src,int nbyte,char** dest)
{
unsigned short* unicode=NULL;
int strlen = UNICODE_encode(src,nbyte,&unicode);
int ret = UTF8_encode(unicode,strlen,dest);
delete[] unicode;
return ret;
}
int UNICODE_encode(const char* src,int bytelen,unsigned short** unicode)
{
if(src == NULL || bytelen == 0 || IsBadReadPtr(src,bytelen) == TRUE)
{
*unicode = NULL;
return 0;
}
int nLen = MultiByteToWideChar(CP_ACP,0,src,bytelen,NULL,NULL);
if(nLen == 0)
{
*unicode = NULL;
return 0;
}
*unicode = new unsigned short[nLen+1];
if(*unicode == NULL || IsBadWritePtr(*unicode,(nLen+1)*sizeof(unsigned short)) == TRUE)
{
*unicode = NULL;
return 0;
}
ZeroMemory(*unicode,nLen*sizeof(unsigned short)+2);
MultiByteToWideChar(CP_ACP,0,src,bytelen,*unicode,nLen);
return nLen;
}
int UTF8_encode(const unsigned short* src,int srclen,char** multibyte)
{
if(src == NULL || srclen == 0 || IsBadReadPtr(src,srclen*sizeof(unsigned short)) == TRUE)
{
*multibyte = NULL;
return 0;
}
int nLen = WideCharToMultiByte(CP_UTF8,0,src,srclen,*multibyte,0,NULL,NULL);
if(nLen == 0)
{
*multibyte = NULL;
return 0;
}
*multibyte = new char[nLen+1];
if(*multibyte == NULL || IsBadWritePtr(*multibyte,nLen+1) == TRUE)
{
*multibyte = NULL;
return 0;
}
ZeroMemory(*multibyte,nLen+1);
WideCharToMultiByte(CP_UTF8,0,src,srclen,*multibyte,nLen,NULL,NULL);
return nLen;
}
int UTF8ToMultibyte(const char* src,int nbyte,char** dest)
{
unsigned short* unicode=NULL;
int strlen = UTF8_decode(src,nbyte,&unicode);
int ret = UNICODE_decode(unicode,strlen,dest);
delete[] unicode;
return ret;
}
int UNICODE_decode(const unsigned short* src,int srclen,char** multibyte)
{
if(src == NULL || srclen == 0 || IsBadReadPtr(src,srclen*sizeof(unsigned short)) == TRUE)
{
*multibyte = NULL;
return 0;
}
int nLen = WideCharToMultiByte(CP_ACP,0,src,srclen,*multibyte,0,NULL,NULL);
if(nLen == 0)
{
*multibyte = NULL;
return 0;
}
*multibyte = new char[nLen+1];
if(*multibyte == NULL || IsBadWritePtr(*multibyte,nLen+1) == TRUE)
{
*multibyte = NULL;
return 0;
}
ZeroMemory(*multibyte,nLen+1);
WideCharToMultiByte(CP_ACP,0,src,srclen,*multibyte,nLen,NULL,NULL);
return nLen;
}
int UTF8_decode(const char* src,int bytelen,unsigned short** unicode)
{
if(src == NULL || bytelen == 0 || IsBadReadPtr(src,bytelen) == TRUE)
{
*unicode = NULL;
return 0;
}
int nLen = MultiByteToWideChar(CP_UTF8,0,src,bytelen,NULL,NULL);
if(nLen == 0)
{
*unicode = NULL;
return 0;
}
*unicode = new unsigned short[nLen+1];
if(*unicode == NULL || IsBadWritePtr(*unicode,(nLen+1)*sizeof(unsigned short)) == TRUE)
{
*unicode = NULL;
return 0;
}
ZeroMemory(*unicode,nLen*sizeof(unsigned short)+2);
MultiByteToWideChar(CP_UTF8,0,src,bytelen,*unicode,nLen);
return nLen;
}
'IT-개발,DB' 카테고리의 다른 글
[개발/VC++] Visual C++에 관련된 유용한 코드 팁 (0) | 2011.02.22 |
---|---|
[개발/VC++] UTF8Decoding, UTF8Encoding (0) | 2011.02.21 |
[개발/VC++] API를 이용한 ASCII와 UNICODE변환및 코드페이지를 이용한 변환방법 (0) | 2011.02.17 |
[개발/VC++] CreateProcess 함수 예제 소스코드 (0) | 2011.02.16 |
[개발/VC++/MFC] CreateProcess로 창숨기기 (0) | 2011.02.16 |
댓글