Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bytearray substitution
#1
Hi everyone!

I want to make an array of bytes like this: my_array= b'\x01\x00\x00\x01\xff\xff'
Then substitute the 4th element to b'\x50'
if I have a int variable A=50 how I can do to change the 4th element to A

I have tried A=A.to_bytes() and then myarray[3]=A but it didnt work.

any ideas of how to do this?

thank you.
Reply
#2
That is not a byte array, rather it is a byte object which is immutable. In order to be able to assign new values, you need to make it into a byte array first. my_array = bytearray(b'\x01\x00\x00\x01\xff\xff')
Now you can assign values to the array my_array[3] = A
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  send bytearray over UART trix 9 1,577 Sep-30-2024, 01:08 PM
Last Post: trix
  bytearray weirdness or newbee newness Curbie 3 1,084 Sep-18-2024, 01:25 AM
Last Post: deanhystad
  Variable Substitution call keys Bobbee 15 2,590 Aug-28-2024, 01:52 PM
Last Post: Bobbee
  Value error when converting hex value to bytearray shubhamjainj 7 13,170 Mar-20-2023, 05:30 PM
Last Post: Skaperen
  appending to a bytearray Skaperen 21 33,391 Mar-19-2023, 11:05 PM
Last Post: Skaperen
  Split Bytearray into separate Files by Hex delimter lastyle 5 6,446 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  Save multiple Parts of Bytearray to File ? lastyle 1 1,549 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  bytearray object - why converting to ascii? Chepilo 2 4,647 Nov-21-2022, 07:25 PM
Last Post: Chepilo
  Need help with for loop and variable value substitution in a function rsurathu 2 3,046 Jul-21-2020, 06:47 AM
Last Post: rsurathu
  Substitution with regular expression returns hidden character SOH bajacri 2 4,774 Nov-17-2019, 03:38 AM
Last Post: bajacri

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020