这段代码不仅仅是含有UTF8到Unicode的转换,还包括Gb2312,Unicode 到Gb2312 如过不使用Windows API的话,那么自己因该建立一个转化表,这样效率才会好,因为我一时没有找到这种表。如果谁有请贴上来。谢谢。
#include
<
string
.h
>
#include
<
tchar.h
>
using
namespace
std;
int
GetNo(
char
ch)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
int i = 0;
while (ch & 0x80)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
++i;
ch <<= 1;
}
return i == 0 ? 1 : i;
}
void
UTF8ToUnicode(
const
char
*
pUtf8,
char
*
pUnicode,
int
nBytes)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
switch (nBytes)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
case 1:
*pUnicode = *pUtf8;
pUnicode += 1;
break;
case 2:
*pUnicode = ((*(pUtf8 + 1)) & 0x3) << 6 | (*pUtf8) & 0x3f;
*(pUnicode + 1) = ((*pUtf8) & 0x1f) >> 2;
pUnicode += 2;
break;
case 3:
*pUnicode = ((*(pUtf8 + 1)) & 0x3) << 6 | ((*(pUtf8 + 2)) & 0x3f);
*(pUnicode + 1) = ((*pUtf8) & 0xf) << 4 | (((*(pUtf8 + 1)) & 0x3f) >> 2);
pUnicode += 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
default:
break;
}
*pUnicode = '';
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int
UnicodeToGB2312(
char
*
pUnicode,
char
*
pGb2312,
int
nBytes)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
int nMultiBytes =::WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, (LPCWSTR)(pUnicode), -1,
pGb2312, 0, NULL, NULL);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (nMultiBytes >= nBytes)
return -1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int nError = ::WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, (LPCWSTR)(pUnicode), -1,
pGb2312, nBytes, NULL, NULL);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (nError == 0)
return -1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return nMultiBytes - 1;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
BOOL UTF8ToGb2312(
const
char
*
pUtf8,
char
*
pGb2312)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
while (*pUtf8 != '')
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int nBytes = GetNo(*pUtf8);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
TCHAR szUnicode[5] = ...{0};
UTF8ToUnicode(pUtf8, szUnicode, nBytes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
TCHAR szGb2312[5] = ...{0};
int nSize = UnicodeToGB2312(szUnicode, szGb2312,
sizeof(szGb2312) - sizeof(szGb2312[0]));
if (nSize == -1 || nSize == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return FALSE;
}
CopyMemory(pGb2312, szGb2312, nSize);
pUtf8 += nBytes;
pGb2312 += nSize ;
}
return TRUE;
}