首页 > 代码库 > UTF-8编码与Unicode CS2的转换
UTF-8编码与Unicode CS2的转换
/* Convert a UTF-8 string into a UCS-2 array. */void tcstrutftoucs(const char *str, uint16_t *ary, int *np){ assert(str && ary && np); const unsigned char *rp = (unsigned char *)str; unsigned int wi = 0; while(*rp != ‘\0‘){ int c = *(unsigned char *)rp; if(c < 0x80){ ary[wi++] = c; } else if(c < 0xe0){ if(rp[1] >= 0x80){ ary[wi++] = ((rp[0] & 0x1f) << 6) | (rp[1] & 0x3f); rp++; } } else if(c < 0xf0){ if(rp[1] >= 0x80 && rp[2] >= 0x80){ ary[wi++] = ((rp[0] & 0xf) << 12) | ((rp[1] & 0x3f) << 6) | (rp[2] & 0x3f); rp += 2; } } rp++; } *np = wi;}/* Convert a UCS-2 array into a UTF-8 string. */int tcstrucstoutf(const uint16_t *ary, int num, char *str){ assert(ary && num >= 0 && str); unsigned char *wp = (unsigned char *)str; for(int i = 0; i < num; i++){ unsigned int c = ary[i]; if(c < 0x80){ *(wp++) = c; } else if(c < 0x800){ *(wp++) = 0xc0 | (c >> 6); *(wp++) = 0x80 | (c & 0x3f); } else { *(wp++) = 0xe0 | (c >> 12); *(wp++) = 0x80 | ((c & 0xfff) >> 6); *(wp++) = 0x80 | (c & 0x3f); } } *wp = ‘\0‘; return (char *)wp - str;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。