Convert Python byte to “unsigned 8 bit integer”

前端 未结 3 1341
执念已碎
执念已碎 2020-12-16 11:22

I am reading in a byte array/list from socket. I want Python to treat the first byte as an \"unsigned 8 bit integer\". How is it possible to get its integer value as an un

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-16 11:59

    bytes/bytearray is a sequence of integers. If you just access an element by its index you'll have an integer:

    >>> b'abc'
    b'abc'
    >>> _[0]
    97
    

    By their very definition, bytes and bytearrays contain integers in the range(0, 256). So they're "unsigned 8-bit integers".

提交回复
热议问题