首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++实现的UTF8编码类,高分相送,回帖者有分,顶起

2012-05-28 
求一个C++实现的UTF8编码类,高分相送,回帖者有分,顶起求一个C++实现的UTF8编码类google好了几个出来,都不

求一个C++实现的UTF8编码类,高分相送,回帖者有分,顶起
求一个C++实现的UTF8编码类


google好了几个出来,都不好用,教高手贴段代码,高分相送,回帖者有分,顶起

[解决办法]

C/C++ code
void Gb2312ToUnicode(WCHAR* pOut, const char *gbBuffer){    ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);    return;}void UnicodeToUTF_8(char* pOut,WCHAR* pText){    // 注意 WCHAR高低字的顺序,低字节在前,高字节在后    char* pchar = (char *)pText;        pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));    //    pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[1] & 0xC0) >> 6);    pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);    pOut[2] = (0x80 | (pchar[0] & 0x3F));        return;}void ConvertGB2312ToUTF_8(char* pOut, int& nOutlen, const char *pText, int pLen){    if ( pOut == NULL || pText == NULL )     {        return;    }        char buf[4] = {0};        int i = 0;    int j = 0;        while(i < pLen)    {        //如果是英文直接复制就可以        if( *(pText + i) >= 0)        {            pOut[j++] = pText[i++];        }        else        {            WCHAR pbuffer;            Gb2312ToUnicode(&pbuffer,pText+i);                        UnicodeToUTF_8(buf,&pbuffer);                        unsigned short int tmp = 0;            tmp = pOut[j] = buf[0];            tmp = pOut[j+1] = buf[1];            tmp = pOut[j+2] = buf[2];                                    j += 3;            i += 2;        }    }    pOut[j] = '\0';        nOutlen = j;}void UnicodeToGB2312(char* pOut,unsigned short uData){    WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(WCHAR),NULL,NULL);    return;}void UTF_8ToUnicode(WCHAR* 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 UTF_8ToGB2312(char *pText, int pLen, char* pOut, int& nOutlen){    if((NULL == pText) || (0 == pLen) || ( NULL == pOut ))    {        return;    }        char Ctemp[4] = {0};    int i =0;    int j = 0;        while(i < pLen)    {        if(pText[i] > 0)        {            pOut[j++] = pText[i++];        }        else                         {            WCHAR Wtemp;            UTF_8ToUnicode(&Wtemp,pText + i);                        UnicodeToGB2312(Ctemp,Wtemp);                        pOut[j] = Ctemp[0];            pOut[j+1] = Ctemp[1];                        i += 3;                j += 2;           }    }        pOut[j] = '\0';    nOutlen = j;}
[解决办法]
iconv.
[解决办法]
iconv 和 楼上的楼上都行 ,用的简单的就用2L的
[解决办法]
顶起。貌似有人给出源码了
[解决办法]
这是我以前用iconv写的字符集转换代码:
C/C++ code
#include <jni.h>#include <iconv.h>#include <string>#include <iostream> #pragma comment(lib,"iconv.lib")int code_convert(char *from_charset,char *to_charset,const char *inbuf, size_t inlen,char *outbuf, size_t outlen){         iconv_t cd;         const char **pin = &inbuf;         char **pout = &outbuf;          cd = iconv_open(to_charset,from_charset);         if (cd==0) return -1;         memset(outbuf,0,outlen);         if (iconv(cd, pin, &inlen,pout, &outlen)==-1) return -1;         iconv_close(cd);         return 0;} /* UTF-8 to GB2312  */int u2g(const char *inbuf, size_t inlen, char *outbuf, size_t outlen){         return code_convert("UTF-8","GB2312",inbuf,inlen,outbuf,outlen);} /* GB2312 to UTF-8 */int g2u(const char *inbuf, size_t inlen, char *outbuf, size_t outlen){         return code_convert("GB2312", "UTF-8", inbuf, inlen, outbuf, outlen);} 

热点排行