Domanda

The other way is simple int(byte_buffer.encode('hex'), 16 )

But how do I convert that integer back to byte_buffer.

The length will be stored by prepending struct.pack('>I', len(byte_buffer)) to the value.

In 2.7 there is int.bit_length() that would be a good start, but unfortunately I must be able to run this also on 2.6.

È stato utile?

Soluzione

This is what I came up with.

def int2str(i):
    _bytes = list()
    while i > 0:
        n = i % 256
        _bytes.insert(0, n)
        i = i >> 8
    return ''.join(struct.pack('B', x) for x in _bytes)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top