search

Home  >  Q&A  >  body text

How to divide python bytes into several bytes

For example, a is a bytes composed of 8 bytes
a=b'/x00/x01/x02/x03/x04/x05/x06/x07'

How do I extract the first four bytes of a=to form a new bytes

I tried the following method
for i in range(0,4):

c[i]=a[i]

But the result c is a list not bytes

But it doesn’t work a[i] is of type int
How to make c equal to the first 4 bytes of extracted a
That is, the result is c=b'/x00/x01/x02/x03'

黄舟黄舟2749 days ago887

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-18 10:49:58

    reply
    0
  • 高洛峰

    高洛峰2017-05-18 10:49:58

    ,不是 /

    >>> a=b'\x00\x01\x02\x03\x04\x05\x06\x07'
    >>> len(a)
    8
    >>> a[:4]
    b'\x00\x01\x02\x03'

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-18 10:49:58

    Thank you I am a beginner haha

    reply
    0
  • Cancelreply