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
  Value error when converting hex value to bytearray shubhamjainj 7 10,472 Mar-20-2023, 05:30 PM
Last Post: Skaperen
  appending to a bytearray Skaperen 21 14,159 Mar-19-2023, 11:05 PM
Last Post: Skaperen
  Split Bytearray into separate Files by Hex delimter lastyle 5 2,625 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  Save multiple Parts of Bytearray to File ? lastyle 1 936 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  bytearray object - why converting to ascii? Chepilo 2 1,610 Nov-21-2022, 07:25 PM
Last Post: Chepilo
  Need help with for loop and variable value substitution in a function rsurathu 2 2,379 Jul-21-2020, 06:47 AM
Last Post: rsurathu
  Substitution with regular expression returns hidden character SOH bajacri 2 3,805 Nov-17-2019, 03:38 AM
Last Post: bajacri
  int() function with bytearray() Jona66 1 2,388 Sep-08-2019, 12:41 PM
Last Post: ichabod801
  Conversion needed from bytearray to Floating point braveYug 1 4,103 May-07-2018, 12:23 PM
Last Post: snippsat
  String substitution with dictionary - AttributeError: 'NoneType' Atalanttore 2 3,249 May-02-2018, 08:52 PM
Last Post: Atalanttore

Forum Jump:

User Panel Messages

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