Varint.c
Read and write variable sized integers in C
Install / Use
/learn @tidwall/Varint.cREADME
varint.c
Read and write variable sized integers in C.
Compatible with Protobuf and Go varints.
API
// read uint64 or int64 from data. returns the number of bytes read or returns
// the zero if there is not enough data or returns -1 if the data does not
// represent a valid varint.
int varint_read_u64(const void *data, size_t len, uint64_t *x);
int varint_read_i64(const void *data, size_t len, int64_t *x);
// write a uint64 or int64 to data buffer. The data buffer must be at least
// 10 bytes in order to hold the maximum sized varint. Returns the number of
// bytes written.
int varint_write_u64(void *data, uint64_t x);
int varint_write_i64(void *data, int64_t x);
Example
#include <stdio.h>
#include "varint.h"
int main() {
char buf[100];
int n;
int64_t i;
uint64_t u;
// write and read a signed integer
i = -305;
n = varint_write_i64(buf, i);
printf("wrote the value %lld (%d bytes)\n", (long long)i, n);
n = varint_read_i64(buf, sizeof(buf), &i);
printf("read the value %lld (%d bytes)\n", (long long)i, n);
// write and read an unsigned integer
u = 102993;
n = varint_write_u64(buf, u);
printf("wrote the value %llu (%d bytes)\n", (unsigned long long)u, n);
n = varint_read_u64(buf, sizeof(buf), &u);
printf("read the value %lld (%d bytes)\n", (unsigned long long)u, n);
}
// output:
// wrote the value -305 (2 bytes)
// read the value -305 (2 bytes)
// wrote the value 102993 (3 bytes)
// read the value 102993 (3 bytes)
Running tests
$ cc varint.c test.c && ./a.out
Related Skills
node-connect
346.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
346.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
346.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
