美文网首页
代码片段: Unicode与UTF-8互转

代码片段: Unicode与UTF-8互转

作者: 客昂康 | 来源:发表于2021-07-08 12:14 被阅读0次
    #include <stdint.h>
    
    // Unicode 转 UTF-8
    int unicode_to_utf8(uint8_t *utf8Buffer, uint16_t *unicodeBuffer, int unicodeNum, uint8_t flag){
        int utf8Length = 0;
        uint16_t unicode;
        while(unicodeNum-- > 0){
            unicode = *(unicodeBuffer++);
            if(unicode <= 0x007f){
                utf8Buffer[0] = unicode & 0x007f;
                utf8Buffer += 1;
                utf8Length += 1;
            }else if(unicode <= 0x07ff){
                utf8Buffer[0] = 0xc0 | ((unicode>>6) & 0x001f);   //6-10位
                utf8Buffer[1] = 0x80 | (unicode & 0x003f);        //0-5位
                utf8Buffer += 2;
                utf8Length += 2;
            }else{
                utf8Buffer[0] = 0xe0 | ((unicode>>12) & 0x000f);  //12-15位
                utf8Buffer[1] = 0x80 | ((unicode>>6)  & 0x003f);  //6-11位
                utf8Buffer[2] = 0x80 | (unicode & 0x003f);        //0-5位
                utf8Buffer += 3;
                utf8Length += 3;
            }
        }
        if(flag){
            utf8Buffer[0] = 0;
            return utf8Length + 1;
        }else{
            return utf8Length;
        }
    }
    
    // UTF-8 转 Unicode
    int utf8_to_unicode(uint16_t *unicodeBuffer, uint8_t *utf8Buffer, int utf8Length, uint8_t flag){
        int unicodeNum = 0;
        while(utf8Length > 0){
            if(utf8Buffer[0] < 0xc0){
                unicodeBuffer[unicodeNum++] = utf8Buffer[0];
                utf8Buffer += 1;
                utf8Length -= 1;
            }else if(utf8Buffer[0] < 0xe0){
                unicodeBuffer[unicodeNum++] = ((uint16_t)(utf8Buffer[0] & 0x1f) << 6) | (utf8Buffer[1] & 0x3f);
                utf8Buffer += 2;
                utf8Length -= 2;
            }else if(utf8Buffer[0] < 0xf0){
                unicodeBuffer[unicodeNum++] = ((uint16_t)(utf8Buffer[0] & 0x0f) << 12) | ((utf8Buffer[1] & 0x3f) << 6) | (utf8Buffer[2] & 0x3f);
                utf8Buffer += 3;
                utf8Length -= 3;
            }else{
                utf8Buffer += 1;
                utf8Length -= 1;
            }
        }
        if(flag){
            unicodeBuffer[unicodeNum++] = 0;
        }
        return unicodeNum + unicodeNum;
    }
    

    相关文章

      网友评论

          本文标题:代码片段: Unicode与UTF-8互转

          本文链接:https://www.haomeiwen.com/subject/itzzultx.html