69,337
社区成员
发帖
与我相关
我的任务
分享
/************************************************************************/
/* Author: NadOo (nadoo@21cn.com)
Reference:
http://blog.csdn.net/lovekatherine/archive/2007/08/30/1765903.aspx
Testing Tool:
http://www.hanzify.org/?Go=Show::List&ID=9627
*/
/************************************************************************/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <memory.h>
#ifdef WIN32
#define uint8_t unsigned __int8
#define uint16_t unsigned __int16
#define uint32_t unsigned __int32
#define uint64_t unsigned __int64
#define int8_t __int8
#define int16_t __int16
#define int32_t __int32
#endif
int unicode_to_utf8(uint16_t *in, int insize, uint8_t **out)
{
int i = 0;
int outsize = 0;
int charscount = 0;
uint8_t *result = NULL;
uint8_t *tmp = NULL;
charscount = insize / sizeof(uint16_t);
result = (uint8_t *)malloc(charscount * 3 + 1);
memset(result, 0, charscount * 3 + 1);
tmp = result;
for (i = 0; i < charscount; i++)
{
uint16_t unicode = in[i];
if (unicode >= 0x0000 && unicode <= 0x007f)
{
*tmp = (uint8_t)unicode;
tmp += 1;
outsize += 1;
}
else if (unicode >= 0x0080 && unicode <= 0x07ff)
{
*tmp = 0xc0 | (unicode >> 6);
tmp += 1;
*tmp = 0x80 | (unicode & (0xff >> 2));
tmp += 1;
outsize += 2;
}
else if (unicode >= 0x0800 && unicode <= 0xffff)
{
*tmp = 0xe0 | (unicode >> 12);
tmp += 1;
*tmp = 0x80 | (unicode >> 6 & 0x00ff);
tmp += 1;
*tmp = 0x80 | (unicode & (0xff >> 2));
tmp += 1;
outsize += 3;
}
}
*tmp = '\0';
*out = result;
return 0;
}
int utf8_to_unicode(uint8_t *in, uint16_t **out, int *outsize)
{
uint8_t *p = in;
uint16_t *result = NULL;
int resultsize = 0;
uint8_t *tmp = NULL;
result = (uint16_t *)malloc(strlen(in) * 2 + 2); /* should be enough */
memset(result, 0, strlen(in) * 2 + 2);
tmp = (uint8_t *)result;
while(*p)
{
if (*p >= 0x00 && *p <= 0x7f)
{
*tmp = *p;
tmp++;
*tmp = '\0';
resultsize += 2;
}
else if ((*p & (0xff << 5))== 0xc0)
{
uint16_t t = 0;
uint8_t t1 = 0;
uint8_t t2 = 0;
t1 = *p & (0xff >> 3);
p++;
t2 = *p & (0xff >> 2);
*tmp = t2 | ((t1 & (0xff >> 6)) << 6);//t1 >> 2;
tmp++;
*tmp = t1 >> 2;//t2 | ((t1 & (0xff >> 6)) << 6);
tmp++;
resultsize += 2;
}
else if ((*p & (0xff << 4))== 0xe0)
{
uint16_t t = 0;
uint8_t t1 = 0;
uint8_t t2 = 0;
uint8_t t3 = 0;
t1 = *p & (0xff >> 3);
p++;
t2 = *p & (0xff >> 2);
p++;
t3 = *p & (0xff >> 2);
//Little Endian
*tmp = ((t2 & (0xff >> 6)) << 6) | t3;//(t1 << 4) | (t2 >> 2);
tmp++;
*tmp = (t1 << 4) | (t2 >> 2);//((t2 & (0xff >> 6)) << 6) | t3;
tmp++;
resultsize += 2;
}
p++;
}
*tmp = '\0';
tmp++;
*tmp = '\0';
resultsize += 2;
*out = result;
*outsize = resultsize;
return 0;
}
void dump_utf8(uint8_t *utf8)
{
uint8_t *p = utf8;
while(*p)
{
printf("%02X", *p);
p++;
}
putchar('\n');
}
void dump_unicode(uint16_t *utf16, int size)
{
uint8_t *p = (uint8_t *)utf16;
int i = 0;
for (i = 0; i < size; i++)
{
printf("%02X", *p);
p++;
}
putchar('\n');
}
int main()
{
uint16_t unicode[] = L"中文";
uint8_t *utf8 = NULL;
int unisize = 0;
uint16_t *uni = NULL;
printf("original unicode: \n");
dump_unicode(unicode, sizeof(unicode));
printf("converted to utf8: \n");
unicode_to_utf8(unicode, sizeof(unicode), &utf8);
dump_utf8(utf8);
printf("converted to unicode: \n");
utf8_to_unicode(utf8, &uni, &unisize);
dump_unicode(uni, unisize);
return 0;
}
#include <string>
using namespace std;
class CChineseCode
{
public:
static void UTF_8ToUnicode(wchar_t* pOut,char *pText); // 把UTF-8转换成Unicode
static void UnicodeToUTF_8(char* pOut,wchar_t* pText); //Unicode 转换成UTF-8
static void UnicodeToGB2312(char* pOut,wchar_t uData); // 把Unicode 转换成 GB2312
static void Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer);// GB2312 转换成 Unicode
static void GB2312ToUTF_8(string& pOut,char *pText, int pLen);//GB2312 转为 UTF-8
static void UTF_8ToGB2312(string &pOut, char *pText, int pLen);//UTF-8 转为 GB2312
};
void CChineseCode::UTF_8ToUnicode(wchar_t* pOut,char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return;
}
void CChineseCode::UnicodeToUTF_8(char* pOut,wchar_t* pText)
{
// 注意 WCHAR高低字的顺序,低字节在前,高字节在后
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));
return;
}
void CChineseCode::UnicodeToGB2312(char* pOut,wchar_t uData)
{
WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(wchar_t),NULL,NULL);
return;
}
void CChineseCode::Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
return ;
}
void CChineseCode::GB2312ToUTF_8(string& pOut,char *pText, int pLen)
{
char buf[4];
int nLength = pLen* 3;
char* rst = new char[nLength];
memset(buf,0,4);
memset(rst,0,nLength);
int i = 0;
int j = 0;
while(i < pLen)
{
//如果是英文直接复制就可以
if( *(pText + i) >= 0)
{
rst[j++] = pText[i++];
}
else
{
wchar_t pbuffer;
Gb2312ToUnicode(&pbuffer,pText+i);
UnicodeToUTF_8(buf,&pbuffer);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j+1] = buf[1];
tmp = rst[j+2] = buf[2];
j += 3;
i += 2;
}
}
rst[j] = '\0';
//返回结果
pOut = rst;
delete []rst;
return;
}
void CChineseCode::UTF_8ToGB2312(string &pOut, char *pText, int pLen)
{
char * newBuf = new char[pLen];
char Ctemp[4];
memset(Ctemp,0,4);
int i =0;
int j = 0;
while(i < pLen)
{
if(pText[i] > 0)
{
newBuf[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(Ctemp,Wtemp);
newBuf[j] = Ctemp[0];
newBuf[j + 1] = Ctemp[1];
i += 3;
j += 2;
}
}
newBuf[j] = '\0';
pOut = newBuf;
delete []newBuf;
return;
}