1

type(request)

return 'bytes',

type(request[5])

returns 'int' When I try to this

request[5] = request[5] + 1

I get this error:

    request[5] = request[5] + 1
TypeError: 'bytes' object does not support item assignment

How to increase "1" this member of bytes ?

3
  • Note that representing individual bytes as numbers is merely a throwback to earlier models of working with raw data. Individual bytes do not represent actual numbers on which numeric operations make sense – for example, b"\xff"[0]+1 does not produce valid bytes data. Is there a reason why you need to perform such operations? Commented Mar 8, 2021 at 12:22
  • Does this answer your question? Item assignment to bytes object? Commented Mar 8, 2021 at 12:25
  • request = request[:5] + bytes((request[5] + 1,)) + request[6:] Commented Mar 9, 2021 at 7:23

1 Answer 1

4

What you want to do is to change the value of a byte type data. byte can NOT be modified. But you can modified it after making it into a bytearray. To do so:

by = b'abcd\x65'
print(b'abcde')
print(type(by))
# b'abcde'
# <class 'bytes'>
bytearray(b'bbcde')
barr = bytearray(by)
print(barr[0])
print(type(barr[0]))
# 97
# <class 'int'>
barr[0] = barr[0]+1
print(barr)
# bytearray(b'bbcde')

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.