<p>A non-negative signed varint is just a normal varint but should be put into a signed integer rather than an unsigned integer as with normal varints. You just need to also check that it is a non-negative integer.</p>

<p>For example, instead of decoding a 32 bit varint as a <code>uint32_t</code>, you decode it as <code>int32_t</code> and check that it's value is <code>&gt;= 0</code>.</p>

<hr>

<p>Your code looks like it is decoding things as signed integers already. Instead of using Python's conversions built into the various classes, I recommend that you use the <a href="https://docs.python.org/3/library/struct.html" rel="nofollow noreferrer"><code>struct</code></a> module so that you can specify endianness and signedness of the bytes that you are decoding.</p>

CC BY-SA 4.0
2019-06-09T21:49:35.440Z
2019-06-09T21:49:35.440Z
Ava Chow
<p>So I'm trying to read the block files with Python.</p>

<p>From this link,
<a href="https://bitcoin.stackexchange.com/questions/28168/what-are-the-keys-used-in-the-blockchain-leveldb-ie-what-are-the-keyvalue-pair">What are the keys used in the blockchain levelDB (ie what are the key:value pairs)?</a>, it tells you how to access the files with leveldb. So I wrote a class to read in the meta data.</p>

<pre><code>class ReadBytes:
    def __init__(self, bytes):
        self.posistion = 0
        self.bytes = bytes

    def get_int(self, byte_count):
        self.posistion += byte_count
        return decode(self.bytes[self.posistion - byte_count:self.posistion][::-1], 256)

    def get_var_int(self):
        self.posistion += 1
        value = byte_to_int(self.bytes[self.posistion - 1])
        if value &lt; 253:
            return value
        return self.get_int(pow(2, value - 252))
</code></pre>

<p>But from this link, <a href="https://github.com/bitcoin/bitcoin/blob/master/src/chain.h#L364" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/master/src/chain.h#L364</a>, It turns out there is this thing call <code>VarIntMode::NONNEGATIVE_SIGNED</code> that is tripping me up. I can read regular var int with my Python class, but how do I read the non-negative signed ones in Python?</p>

<block><python><leveldb>
How to parse values in block index leveldb?
guest1234312
# How to parse values in block index leveldb?

(Asked by: username_0 on 2019-06-09)

So I'm trying to read the block files with Python.

From this link,
[What are the keys used in the blockchain levelDB (ie what are the key:value pairs)?](https://bitcoin.stackexchange.com/questions/28168/what-are-the-keys-used-in-the-blockchain-leveldb-ie-what-are-the-keyvalue-pair), it tells you how to access the files with leveldb. So I wrote a class to read in the meta data.

```
class ReadBytes:
    def __init__(self, bytes):
        self.posistion = 0
        self.bytes = bytes

    def get_int(self, byte_count):
        self.posistion += byte_count
        return decode(self.bytes[self.posistion - byte_count:self.posistion][::-1], 256)

    def get_var_int(self):
        self.posistion += 1
        value = byte_to_int(self.bytes[self.posistion - 1])
        if value < 253:
            return value
        return self.get_int(pow(2, value - 252))

```

But from this link, <https://github.com/bitcoin/bitcoin/blob/master/src/chain.h#L364>, It turns out there is this thing call `VarIntMode::NONNEGATIVE_SIGNED` that is tripping me up. I can read regular var int with my Python class, but how do I read the non-negative signed ones in Python?

**Tags:** block, python, leveldb


---

## 1 answers

### Answer by username_1 on 2019-06-09. Score: 0 | Accepted answer

A non-negative signed varint is just a normal varint but should be put into a signed integer rather than an unsigned integer as with normal varints. You just need to also check that it is a non-negative integer.

For example, instead of decoding a 32 bit varint as a `uint32_t`, you decode it as `int32_t` and check that it's value is `>= 0`.

---

Your code looks like it is decoding things as signed integers already. Instead of using Python's conversions built into the various classes, I recommend that you use the [`struct`](https://docs.python.org/3/library/struct.html) module so that you can specify endianness and signedness of the bytes that you are decoding.


---

