如何在Objective-C中将int转换为VLQ,基本上我可以像http://code.google.com/apis/protocolbuffers/docs/encoding.html
那样得到Variant我发现了这个(来自http://rosettacode.org/wiki/Variable-length_quantity)并想知道如何将其转换为Objective-C?
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define BUFLEN 8
#define MAX_UINT32_AS_VLQ 8
size_t uint32_to_vlq(uint32_t number, uint8_t *buf, size_t buflen)
{
uint8_t ibuf[MAX_UINT32_AS_VLQ];
size_t written = 0, i;
if (buflen == 0) return 0;
if ( number < 128 )
{
*buf = number;
return 1;
}
ibuf[0] = number & 0x7f;
written++;
number >>= 7;
while(number > 0)
{
ibuf[written] = (number & 0x7f) | 0x80;
number >>= 7;
written++;
}
if (written > buflen) return 0;
for(i = 0; i < written; i++) buf[i] = ibuf[written-i-1];
return written;
}
提前致谢
答案 0 :(得分:4)
由于Objective-C是C的超级集,因此您可以直接在Objective-C代码中使用此C代码。