现在的位置: 首页 > 综合 > 正文

字符格式转换

2013年10月25日 ⁄ 综合 ⁄ 共 1907字 ⁄ 字号 评论关闭

static void convertUTF16ToUTF8(AM_INT* index, AM_U8* pUtf8, AM_U16 mUtf16)
{
       AM_INT i = *index;
       if(mUtf16 >= 0x800){
             // 3 bytes, utf-8: 1110xxxx 10xxxxxx 10xxxxxx
             pUtf8[i] = (mUtf16 >> 12) | 0xe0;
             pUtf8[i+1] = (mUtf16 >> 6) & 0x3f | 0x80;
             pUtf8[i+2] = mUtf16 & 0x3f | 0x80;
             i += 3;
       }else if(mUtf16 >= 0x80){
             // 2 bytes, utf-8: 110xxxxx 10xxxxxx
             pUtf8[i] = (mUtf16 >> 6) & 0x1f | 0xc0;
             pUtf8[i+1] = mUtf16 & 0x3f | 0x80;
             i += 2;
       }else {
             // 1 byte, utf-8: 0xxxxxxx
             pUtf8[i] = (AM_U8)mUtf16;
             i += 1;
       }
       *index = i;
}

static AM_U8* convertGB2312ToUnicode(const char* value)
{
       AM_U8* mpUtf8 = NULL;
       AM_U16 mUnicode = 0;
       AM_INT r, c;

       AM_INT max_size = 128;
       AM_INT size = strlen(value);

       if(value[0] == 0xff && value[1] == 0xfe){
              AM_WARNING("formate is Unicode already\n");
              return NULL;
       }

       mpUtf8 = (AM_U8*)malloc(max_size);
       memset(mpUtf8, 0, max_size);

       AM_INT i = 0;
       AM_INT j = 0;
       for(i = 0; i < size; i++){
              if(value[i]&0x80){//GB2312
                   r = (AM_UINT)value[i] - 0xa0;
                   c = (AM_UINT)value[i+1] - 0xa0;
                   if(r < 0 || c < 0){
                        free(mpUtf8);
                        return NULL;
                   }
                   mUnicode = gb2312_to_unicode[r-1][c-1];
                   if(mUnicode == 0xffff){
                        free(mpUtf8);
                        return NULL;
                   }
                   if(j > 124){
                        //the max size is 127 + 1
                        return mpUtf8;
                   }
                   convertUTF16ToUTF8(&j, mpUtf8, mUnicode);
                   i++;
              }else{//ASCII
                   mpUtf8[j] = value[i];
                   j += 1;
                   if(j > 127){
                        //the max size is 127 + 1
                        return mpUtf8;
                   }
              }
       }

       return mpUtf8;
}

抱歉!评论已关闭.