def int_to_varbyte(self, value):
        """Convert an integer into a variable length byte.

        How it works: the bytes are stored in big-endian (significant bit
        first), the highest bit of the byte (mask 0x80) is set when there
        are more bytes following. The remaining 7 bits (mask 0x7F) are used
        to store the value.
        """
        # Warning: bit kung-fu ahead. The length of the integer in bytes
        length = int(log(max(value, 1), 0x80)) + 1

        # Remove the highest bit and move the bits to the right if length > 1
        bytes = [value >> i * 7 & 0x7F for i in range(length)]
        bytes.reverse()

        # Set the first bit on every one but the last bit.
        for i in range(len(bytes) - 1):
            bytes[i] = bytes[i] | 0x80
        return pack('%sB' % len(bytes), *bytes)
Convert an integer into a variable length byte.

        How it works: the bytes are stored in big-endian (significant bit
        first), the highest bit of the byte (mask 0x80) is set when there
        are more bytes following. The remaining 7 bits (mask 0x7F) are used
        to store the value.