# How to parse variable sized integer from metadata log

**URL:** <https://forum.codecrafters.io/t/how-to-parse-variable-sized-integer-from-metadata-log/4254>\
**Category:** Challenges\
**Tags:** challenge:kafka\
**Created:** [January 17, 2025, 3:12pm UTC](https://forum.codecrafters.io/t/how-to-parse-variable-sized-integer-from-metadata-log/4254 "2025-01-17T15:12:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdtran3k6](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/pdtran3k6/32/4124_2.png) [@pdtran3k6](https://forum.codecrafters.io/u/pdtran3k6)\
**Post date:** [January 17, 2025, 3:12pm UTC](https://forum.codecrafters.io/t/how-to-parse-variable-sized-integer-from-metadata-log/4254/1 "2025-01-17T15:12:30Z")

</div>

This is for Stage #EA7.

I’ve been assuming that the any length is 1 byte, but then I saw that the [value length](https://binspec.org/kafka-cluster-metadata?highlight=189-190) of the record is 2 bytes. After further reading, I realized that the spec said its a signed variable size integer. How does one parse a variable sized integer i.e how does one know when to stop reading a variable sized thing?

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [January 17, 2025, 3:35pm UTC](https://forum.codecrafters.io/t/how-to-parse-variable-sized-integer-from-metadata-log/4254/2 "2025-01-17T15:35:17Z")

</div>

Hi @pdtran3k6, here’s an example illustrating [how to parse `0x8147` as decimal 199](https://forum.codecrafters.io/t/sqlite-varint-decoding-stage-sz4/1934/2).

* * *

> how does one know when to stop reading a variable sized thing

Stop when a byte has a leading 0.

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/1/12411099c5f30541c3dcea296f9af9cd93afef2a.jpeg)

0x8201 has two bytes:

- `1`000 0010: continue
- `0`000 0001: stop

---

<div class="post-metadata">

**Author:** ![pdtran3k6](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/pdtran3k6/32/4124_2.png) [@pdtran3k6](https://forum.codecrafters.io/u/pdtran3k6)\
**Post date:** [January 21, 2025, 1:25pm UTC](https://forum.codecrafters.io/t/how-to-parse-variable-sized-integer-from-metadata-log/4254/3 "2025-01-21T13:25:35Z")

</div>

what’s the software that you use here to see the hex in binary like that?

---

<div class="post-metadata">

**Author:** ![pdtran3k6](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/pdtran3k6/32/4124_2.png) [@pdtran3k6](https://forum.codecrafters.io/u/pdtran3k6)\
**Post date:** [January 21, 2025, 1:29pm UTC](https://forum.codecrafters.io/t/how-to-parse-variable-sized-integer-from-metadata-log/4254/4 "2025-01-21T13:29:52Z")

</div>

and also, if I read the other post correctly, in this case, the final number after concat would be:

```auto
000 0010 + 000 0001
= 10 000 0001
= 1 0000 0001 = 257

```

which is definitely not 65 like the link [here](https://binspec.org/kafka-cluster-metadata?highlight=189-190)  
and even if I intepret it as a signed integer, that would be -255, also not 65

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [January 21, 2025, 2:48pm UTC](https://forum.codecrafters.io/t/how-to-parse-variable-sized-integer-from-metadata-log/4254/5 "2025-01-21T14:48:12Z")

</div>

It’s the Calculator app on mac in programmer mode:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/7/76fbd0e5f88d40b60888f3a24421853a60aec703.jpeg)

* * *

You’re absolutely right! It’s 257. I was a bit confused as well, because the length of `Value (Partition Record)` is apparently 65 (= 1 + 16 x 4):

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/a/a6fd2f4fa8656609cd1056fd3de97946d3b4d24a.png)

Turns out that VARINT in Kafka involves two additional considerations:

1. [Small/Big-endian order](https://protobuf.dev/programming-guides/encoding/#varints)
2. [Zig-zag encoding](https://protobuf.dev/programming-guides/encoding/#signed-ints)

```auto
000 0010 + 000 0001 reverses order due to endianness:

000 0001 + 000 0010

= 1 000 0010 = 130

```

130 becomes 130 / 2 = 65 due to zig-zag encoding.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [March 10, 2025, 9:46am UTC](https://forum.codecrafters.io/t/how-to-parse-variable-sized-integer-from-metadata-log/4254/6 "2025-03-10T09:46:58Z")

</div>

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [March 15, 2025, 9:47am UTC](https://forum.codecrafters.io/t/how-to-parse-variable-sized-integer-from-metadata-log/4254/7 "2025-03-15T09:47:19Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
