字符格式转换

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值