How to get an integer from a sequence of bytes

Ian Kelly ian.g.kelly at gmail.com
Thu May 30 14:42:24 EDT 2013


On Thu, May 30, 2013 at 12:26 PM, Mok-Kong Shen
<mok-kong.shen at t-online.de> wrote:
> Am 27.05.2013 17:30, schrieb Ned Batchelder:
>>
>> On 5/27/2013 10:45 AM, Mok-Kong Shen wrote:
>>>
>>> From an int one can use to_bytes to get its individual bytes,
>>> but how can one reconstruct the int from the sequence of bytes?
>>>
>> The next thing in the docs after int.to_bytes is int.from_bytes:
>> http://docs.python.org/3.3/library/stdtypes.html#int.from_bytes
>
>
> I am sorry to have overlooked that. But one thing I yet wonder is why
> there is no direct possibilty of converting a byte to an int in [0,255],
> i.e. with a constrct int(b), where b is a byte.

The bytes object can be viewed as a sequence of ints.  So if b is a
bytes object of non-zero length, then b[0] is an int in range(0, 256).



More information about the Python-list mailing list