#include "stdafx.h"
#include <windows.h>
#include "CharConversion .h"
BOOL Utf8ToUnicode(wchar_t* &pUtf16OutBuff, const char *pUtf8Text)
{
/*
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
*/
WCHAR* pwszBuff = NULL;
INT nLen = 0;
BOOL bRet = FALSE;
if (pUtf8Text == NULL)
{
return TRUE; //
}
nLen = MultiByteToWideChar(CP_UTF8, 0, pUtf8Text, -1, pwszBuff, 0);
pwszBuff = new WCHAR[nLen];
memset(pwszBuff, 0, nLen*sizeof(pwszBuff[0]));
nLen = MultiByteToWideChar(CP_UTF8, 0, pUtf8Text, -1, pwszBuff, nLen*sizeof(pwszBuff[0]));
if (!nLen)
{
delete[]pwszBuff;
bRet = FALSE; //转化failed
}
else
{
pUtf16OutBuff = pwszBuff;
bRet = TRUE;
}
return bRet;
}
BOOL UnicodeToUtf8(char* &pUtf8OutBuff, const wchar_t* pUtf16Text)
{
/*
char* pchar = (char *)pText;
pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
*/
CHAR* pszBuff = NULL;
INT nLen = 0;
BOOL bRet = FALSE;
if (pUtf16Text == NULL)
{
return TRUE; //
}
nLen = WideCharToMultiByte(CP_UTF8, 0, pUtf16Text, -1, pszBuff, 0, NULL, NULL);
pszBuff = new CHAR[nLen];
memset(pszBuff, 0, nLen*sizeof(pszBuff[0]));
nLen = WideCharToMultiByte(CP_UTF8, 0, pUtf16Text, -1, pszBuff, nLen*sizeof(pszBuff[0]), NULL, NULL);
if (!nLen)
{
delete[]pszBuff;
bRet = FALSE; //转化failed
}
else
{
pUtf8OutBuff = pszBuff;
bRet = TRUE;
}
return bRet;
}
BOOL UnicodeToAnsi(char* &pAnsiOutBuf, const wchar_t* pUtf16Text)
{
CHAR* pszBuff = NULL;
INT nLen = 0;
BOOL bRet = FALSE;
if (pUtf16Text == NULL)
{
return TRUE; //
}
nLen = WideCharToMultiByte(CP_ACP, 0, pUtf16Text, -1, pszBuff, 0, NULL, NULL);
pszBuff = new CHAR[nLen];
memset(pszBuff, 0, nLen*sizeof(pszBuff[0]));
nLen = WideCharToMultiByte(CP_ACP, 0, pUtf16Text, -1, pszBuff, nLen*sizeof(pszBuff[0]), NULL, NULL);
if (!nLen)
{
delete[]pszBuff;
bRet = FALSE; //转化failed
}
else
{
pAnsiOutBuf = pszBuff;
bRet = TRUE;
}
return bRet;
}
BOOL AnsiToUnicode(wchar_t* &putf16OutBuff, const char *pAnsiText)
{
WCHAR* pwszBuff = NULL;
INT nLen = 0;
BOOL bRet = FALSE;
if (pAnsiText == NULL)
{
return TRUE; //
}
nLen = MultiByteToWideChar(CP_ACP, 0, pAnsiText, -1, pwszBuff, 0);
pwszBuff = new WCHAR[nLen];
memset(pwszBuff, 0, nLen*sizeof(pwszBuff[0]));
nLen = MultiByteToWideChar(CP_ACP, 0, pAnsiText, -1, pwszBuff, nLen);
if (!nLen)
{
delete[]pwszBuff;
bRet = FALSE; //转化failed
}
else
{
putf16OutBuff = pwszBuff;
bRet = TRUE;
}
return bRet;
}
BOOL Utf8ToAnsi(char * &pAnsiOutBuff, const char* pUtf8Text)
{
wchar_t* putf16Buff = NULL;
BOOL bRet = FALSE;
if (pUtf8Text == NULL)
{
return TRUE;
}
bRet = Utf8ToUnicode(putf16Buff, pUtf8Text);
if (bRet)
{
bRet = UnicodeToAnsi(pAnsiOutBuff, putf16Buff);
}
if (putf16Buff)
{
delete[]putf16Buff;
}
return bRet;
}
BOOL AnsiToUtf8(char * &pUtf8OutBuff, const char* pAnsiText)
{
wchar_t* putf16Buff = NULL;
BOOL bRet = FALSE;
if (pAnsiText == NULL)
{
return TRUE;
}
bRet = AnsiToUnicode(putf16Buff, pAnsiText);
if (bRet)
{
bRet = UnicodeToUtf8(pUtf8OutBuff, putf16Buff);
}
if (putf16Buff)
{
delete[]putf16Buff;
}
return bRet;
}