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

彩信uintvar编解码

2012年11月26日 ⁄ 综合 ⁄ 共 668字 ⁄ 字号 评论关闭

/*uintvar 编码*/

int encodeUintvar(int data, char *pUintVar, int *iUintVarLen){
    char reversed[8]={0};
    char tmp;
    int i = 0;
    reversed[i]=(char)data & 0x7f;   // The lowest
    data = data >> 7;
    i++;
    while ( data > 0 )
    {
        reversed[i] = 0x80 | (data & 0x7f);
        i++;
        data = data >> 7;
    }
    int j;
    // Reverse it because it is in reverse order
    for ( j = 0; j < i; j++ )
    {
        pUintVar[j] = reversed[i - j - 1];
    }
        *iUintVarLen=i;
        return pUintVar;

}

/*uintvar解码*/

int  decodeUintvar(const unsigned char* bytes,  long* value)
{
 int cnt = 0;
 unsigned char v = *bytes++;
 long int v64 = 0;
 while(v & 0x80){
   ++cnt;
   v64|=(v   &   0x7F);
   v64<<=7;   //7  bits
   v=*bytes++;
 }
 v64|= v; //last 7 bits

 *value = v64;
 return (cnt+1);
}

 

 

抱歉!评论已关闭.