[개발/VC++] MultibyteToUTF8, UTF8ToMultibyte
출처 : 인터넷
[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++] IE 보호 모드에서 비보호 모드 프로세스 실행 (0) | 2015.10.08 |
---|---|
[개발/VC++] UTF8Decoding, UTF8Encoding (0) | 2015.10.08 |
[개발/VC++] API를 이용한 ASCII와 UNICODE변환및 코드페이지를 이용한 변환방법 (0) | 2015.10.07 |
[개발/VC++] URLEncode, URLDecode, 유니코드 변환 소스 (0) | 2015.10.07 |
[개발/VC++] URLEncode, URLDecode, UTF8 변환 소스 (0) | 2015.10.07 |
댓글