zoukankan      html  css  js  c++  java
  • 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 != ''){
        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 = '';
      return (char *)wp - str;
    }
  • 相关阅读:
    关于renren vue项目的启动
    idea得破解
    activiti任意节点任务跳转
    activiti的网关(GateWay)
    activiti入门案例
    activiti基本介绍
    事务的传播机制
    设计模式之鸭子模式
    Python中的数据类型
    tp5 模板参数配置:tpl_replace_string
  • 原文地址:https://www.cnblogs.com/feika/p/3847503.html
Copyright © 2011-2022 走看看