Created
September 30, 2013 14:06
-
-
Save skoowoo/6764324 to your computer and use it in GitHub Desktop.
base 128 varint编码解码算法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
int encode_varint(char *buf, uint64_t x) | |
{ | |
int n; | |
n = 0; | |
while (x > 127) { | |
buf[n++] = (char) (0x80 | (x & 0x7F)); | |
x >>= 7; | |
} | |
buf[n++] = (char) x; | |
return n; | |
} | |
uint64_t decode_varint(char *buf) | |
{ | |
int shift, n; | |
uint64_t x, c; | |
n = 0; | |
x = 0; | |
for (shift = 0; shift < 64; shift += 7) { | |
c = (uint64_t) buf[n++]; | |
x |= (c & 0x7F) << shift; | |
if ((c & 0x80) == 0) { | |
break; | |
} | |
} | |
return x; | |
} | |
/* test */ | |
int main() | |
{ | |
char buf[64]; | |
int n; | |
uint64_t x; | |
n = encode_varint(buf, 1300); | |
printf("%d\n", n); | |
x = decode_varint(buf); | |
printf("%d\n", (int) (x)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment