Python Forum
Is there a built-in function to replace multiple bytes?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a built-in function to replace multiple bytes?
#1
I'm learning Python 3.6.

I figured out how to use the find() function to find a multiple hex byte string in a byte variable as follows:

with open("test 18.vf", "rb") as binaryFile:
    byteData = bytearray(binaryFile.read()) 

mx = 0
while mx != -1:
    mx = byteData.find(b'\x2A\x07\x3B\x1C\x14', mx)
    if mx != -1:
        Do something
Is there a built-in function to change consecutive bytes in "byteData" that looks similar to the find function above?
If not, any suggestions on the easiest way to replace 7 consecutive bytes?  I won't be inserting any bytes.  Just replacing 7 consecutive bytes elsewhere in "byteData".

Thanks.
Reply
#2
Untested, but this should work:

replace:
   mx = byteData.find(b'\x2A\x07\x3B\x1C\x14', mx)
with:
   bytedata=butedata.replace(b'\x2A\x07\x3B\x1C\x14', b'New value here')
Reply
#3
(Feb-24-2017, 08:01 PM)Larz60+ Wrote: Untested, but this should work:

replace:
   mx = byteData.find(b'\x2A\x07\x3B\x1C\x14', mx)
with:
   bytedata=butedata.replace(b'\x2A\x07\x3B\x1C\x14', b'New value here')

Thanks Larz60+.  I won't know the bytes to be replaced as they could vary depending on the user's setup.  I neglected to say that I will be replacing the 7 bytes based on the index value of "mx".

For example:
1. Search for the 5 byte string. 
2. If found, mx = index of first found byte.
3. Add a value to mx to skip bytes.  Like mx += 52.
4. Replace 7 bytes starting from the current index value of mx.
5. Repeat steps 1-4 until the end of "byteData" signified by mx == -1.

Sorry for not being more through in my initial post.
Thanks.
Reply
#4
If you want to replace 7 bytes from given index mx, you can do
byteData[mx : mx+7] = 1, 2, 3, 4, 5, 6, 7
bytearray is mutable, so it will work. And if you want these bytes replaced with different number of bytes, you can use .insert() method or del byteData[index1: index2].
Reply
#5
(Feb-24-2017, 11:53 PM)zivoni Wrote: If you want to replace 7 bytes from given index mx, you can do
byteData[mx : mx+7] = 1, 2, 3, 4, 5, 6, 7
bytearray is mutable, so it will work. And if you want these bytes replaced with different number of bytes, you can use .insert() method or del byteData[index1: index2].

Hi zivoni,

So simple, so easy, so clean.

Works perfectly, So thanks!
Raptor88
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad Iterate randint() multiple times when calling a function Jake123 2 2,057 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,154 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Function - Return multiple values tester_V 10 4,451 Jun-02-2021, 05:34 AM
Last Post: tester_V
  print function help percentage and slash (multiple variables) leodavinci1990 3 2,492 Aug-10-2020, 02:51 AM
Last Post: bowlofred
  basic random number generator in replace function krug123 2 2,045 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  How to pass multiple arguments into function Mekala 4 2,458 Jul-11-2020, 07:03 AM
Last Post: Mekala
  Reading Multiple Lists Using SUM function dgrunwal 6 3,355 Jun-03-2020, 08:23 PM
Last Post: dgrunwal
  Using function *args to multiply multiple arguments allusernametaken 8 6,088 Nov-20-2019, 12:01 AM
Last Post: allusernametaken
  Replace last 4 bytes of a timestamp mPlummers 18 7,400 Sep-17-2019, 12:47 AM
Last Post: Larz60+
  Beginner problem, replace function with for loop Motley_Cow 9 4,632 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow

Forum Jump:

User Panel Messages

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