Python Forum
Replace Bytes in File between Value X and Y with Z
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace Bytes in File between Value X and Y with Z
#1
Hi all

I`ve got a File that contains different Byte Values and i need some of them to be exchanged.

Idea is to read the entire File into an Array and then using a routine like :

If byte Value is between 193 and 219 then substract 129 from it and replace with the new Value in the array.

Open File and after exchange that is plain Simple, but how would a Function like this look like ?

Thanks for any Help guiding me there Smile
Reply
#2
What have you tried so far?
Reply
#3
(Feb-04-2020, 06:35 PM)Larz60+ Wrote: What have you tried so far?

to be honest, nothing. I dont know which function i should use for a compare between value x and y

In vb.net i am more experienced and would parse like

Public Sub parsefile(filename As String)
Dim bytes As Byte() = System.IO.File.ReadAllBytes(filename)
For i = 0 To bytes.length
if val(bytes(i)) > 193 < 219 then
val(bytes(i)) = val(bytes(i))-129
Next i
End Sub

But here with python i have no idea, thats why i am hoping somebody would help me
Reply
#4
looks like you need to start with a python tutorial, I'll list a couple of good ones below.
I will show examples for:
  • variables -- in python don't have to be declared before use as in Virtual basic
    x = value
  • opening and reading filename -- is simple
    with open(filename, mode) as name:
            for line in name:
                line = line.strip() # removes whitespace
                ...
  • A text line can be parsed into a list:
       words = line.split()
  • iterate through list:
        for word in listname:
            ...
  • Get numeric value of letter:
       val = ord(word[index])
  • to get letter from numeric:
       letter = chr(val)
  • print any variable:
       print(val, letter)
  • formatted print
       print(f"value of val: {val}, letter: {letter}")
val, letter, x are variables, don't need to be declared

These are both excellent tutorials:
How to Think Like a Computer Scientist: Interactive Edition
Python3 Tutorial
Reply


Forum Jump:

User Panel Messages

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