How to convert while loop to for loop in my code?

Joined
Dec 19, 2024
Messages
7
Reaction score
0
I have a working script in python like this:

Code:
import re

handle_file1 = open("example1.so", "rb")
hex_file1 = handle_file1.read().hex().upper()
handle_file1.close()

handle_file2 = open("example2.so", "rb")
hex_file2 = handle_file2.read().hex().upper()
handle_file2.close()

array_file1 = re.findall('[0-9A-Fa-f]{2}', hex_file1)
array_file2 = re.findall('[0-9A-Fa-f]{2}', hex_file2)

i = 0
while i < len(array_file1):
    if array_file1[i] != array_file2[i]:
        if i % 4 == 0:
            print(array_file1[i] + array_file1[i + 1] + array_file1[i + 2] + array_file1[i + 3] + "\t" + array_file2[i] + array_file2[i + 1] + array_file2[i + 2] + array_file2[i + 3])
            i += 4
        elif i % 4 == 1:
            print(array_file1[i - 1] + array_file1[i] + array_file1[i + 1] + array_file1[i + 2] + "\t" + array_file2[i - 1] + array_file2[i] + array_file2[i + 1] + array_file2[i + 2])
            i += 3
        elif i % 4 == 2:
            print(array_file1[i - 2] + array_file1[i - 1] + array_file1[i] + array_file1[i + 1] + "\t" + array_file2[i - 2] + array_file2[i - 1] + array_file2[i] + array_file2[i + 1])
            i += 2
        elif i % 4 == 3:
            print(array_file1[i - 3] + array_file1[i - 2] + array_file1[i - 1] + array_file1[i] + "\t" + array_file2[i - 3] + array_file2[i - 2] + array_file2[i - 1] + array_file2[i])
            i += 1
    else:
        i += 1

How can I convert the while loop into for loop?
I have AutoIT script like this:

Code:
        For $i = 1 To UBound($aFile1, $UBOUND_ROWS)-1
            If $aFile1[$i] <> $aFile2[$i] Then
                If Mod($i, 4) = 0 Then
                    FileWrite(@ScriptDir & '\' & 'Result.txt', $aFile1[$i] & ' ' & $aFile1[$i+1] & ' ' & $aFile1[$i+2] & ' ' & $aFile1[$i+3] & '    ' & $aFile2[$i] & ' ' & $aFile2[$i+1] & ' ' & $aFile2[$i+2] & ' ' & $aFile2[$i+3] & @CRLF)
                    $i = $i + 4
                ElseIf Mod($i, 4) = 1 Then
                    FileWrite(@ScriptDir & '\' & 'Result.txt', $aFile1[$i-1] & ' ' & $aFile1[$i] & ' ' & $aFile1[$i+1] & ' ' & $aFile1[$i+2] & '    ' & $aFile2[$i-1] & ' ' & $aFile2[$i] & ' ' & $aFile2[$i+1] & ' ' & $aFile2[$i+2] & @CRLF)
                    $i = $i + 3
                ElseIf Mod($i, 4) = 2 Then
                    FileWrite(@ScriptDir & '\' & 'Result.txt', $aFile1[$i-2] & ' ' & $aFile1[$i-1] & ' ' & $aFile1[$i] & ' ' & $aFile1[$i+1] & '    ' & $aFile2[$i-2] & ' ' & $aFile2[$i-1] & ' ' & $aFile2[$i] & ' ' & $aFile2[$i+1] & @CRLF)
                    $i = $i + 2
                ElseIf Mod($i, 4) = 3 Then
                    FileWrite(@ScriptDir & '\' & 'Result.txt', $aFile1[$i-3] & ' ' & $aFile1[$i-2] & ' ' & $aFile1[$i-1] & ' ' & $aFile1[$i] & '    ' & $aFile2[$i-3] & ' ' & $aFile2[$i-2] & ' ' & $aFile2[$i-1] & ' ' & $aFile2[$i] & @CRLF)
                    $i = $i + 1
                EndIf
            ElseIf $aFile1[$i] = $aFile2[$i] Then
                ContinueLoop
            EndIf
       Next

I want to convert my while loop into for loop
or I simply want to convert my for loop from AutoIT to Python

How to do that?

Thank You
 
Joined
Jul 4, 2023
Messages
527
Reaction score
70
Are you sure that the for loop will work better in this case?
Check this out [ run code online ]:
Python:
import random

# Generating two lists with random hex values for demonstration
array_file1 = [f"{random.randint(0, 255):02X}" for _ in range(20)]
array_file2 = [f"{random.randint(0, 255):02X}" for _ in range(20)]

print(array_file1, "\n", array_file2, "\n", sep="")

index_for = []
for i in range(0, len(array_file1)):
    if array_file1[i] != array_file2[i]:
        if i % 4 == 0:
            i += 4
        if i % 4 == 1:
            i += 3
     
    index_for.append(i)

index_while = []
i = 0
while i < len(array_file1):
    if array_file1[i] != array_file2[i]:
        if i % 4 == 0:
            i += 4
        elif i % 4 == 1:
            i += 3
        else:
            i += 1
     
    index_while.append(i)
 
print(f"For index:   {index_for}", "\n", f"While index: {index_while}", sep="")

convert my for loop from AutoIT to Python
Check this out [ run code online ]:
Python:
import re

'''
with open("example1.so", "rb") as handle_file1:
    hex_file1 = handle_file1.read().hex().upper()

with open("example2.so", "rb") as handle_file2:
    hex_file2 = handle_file2.read().hex().upper()

array_file1 = re.findall('[0-9A-Fa-f]{2}', hex_file1)
array_file2 = re.findall('[0-9A-Fa-f]{2}', hex_file2)
'''

# Generating two lists with random hex values for demonstration
import random
array_file1 = [f"{random.randint(0, 255):02X}" for _ in range(20)]
array_file2 = [f"{random.randint(0, 255):02X}" for _ in range(20)]

array_result = []
for i in range(0, len(array_file1)):
    if array_file1[i] != array_file2[i]:
        if i % 4 == 0:
            array_result.append(f"{array_file1[i]} {array_file1[i+1]} {array_file1[i+2]} {array_file1[i+3]}\t{array_file2[i]} {array_file2[i+1]} {array_file2[i+2]} {array_file2[i+3]}")
            i += 4
        elif i % 4 == 1:
            array_result.append(f"{array_file1[i-1]} {array_file1[i]} {array_file1[i+1]} {array_file1[i+2]}\t{array_file2[i-1]} {array_file2[i]} {array_file2[i+1]} {array_file2[i+2]}")
            i += 3
        elif i % 4 == 2:
            array_result.append(f"{array_file1[i-2]} {array_file1[i-1]} {array_file1[i]} {array_file1[i+1]}\t{array_file2[i-2]} {array_file2[i-1]} {array_file2[i]} {array_file2[i+1]}")
            i += 2
        elif i % 4 == 3:
            array_result.append(f"{array_file1[i-3]} {array_file1[i-2]} {array_file1[i-1]} {array_file1[i]}\t{array_file2[i-3]} {array_file2[i-2]} {array_file2[i-1]} {array_file2[i]}")
            i += 1

with open("Result.txt", "w") as file:
    file.write("\n".join(array_result))
 
Last edited:
Joined
Dec 19, 2024
Messages
7
Reaction score
0
Are you sure that the for loop will work better in this case?
Check this out [ run code online ]:
Python:
import random

# Generating two lists with random hex values for demonstration
array_file1 = [f"{random.randint(0, 255):02X}" for _ in range(20)]
array_file2 = [f"{random.randint(0, 255):02X}" for _ in range(20)]

print(array_file1, "\n", array_file2, "\n", sep="")

index_for = []
for i in range(0, len(array_file1)):
    if array_file1[i] != array_file2[i]:
        if i % 4 == 0:
            i += 4
        if i % 4 == 1:
            i += 3
    
    index_for.append(i)

index_while = []
i = 0
while i < len(array_file1):
    if array_file1[i] != array_file2[i]:
        if i % 4 == 0:
            i += 4
        elif i % 4 == 1:
            i += 3
        else:
            i += 1
    
    index_while.append(i)
 
print(f"For index:   {index_for}", "\n", f"While index: {index_while}", sep="")


Check this out [ run code online ]:
Python:
import re

'''
with open("example1.so", "rb") as handle_file1:
    hex_file1 = handle_file1.read().hex().upper()

with open("example2.so", "rb") as handle_file2:
    hex_file2 = handle_file2.read().hex().upper()

array_file1 = re.findall('[0-9A-Fa-f]{2}', hex_file1)
array_file2 = re.findall('[0-9A-Fa-f]{2}', hex_file2)
'''

# Generating two lists with random hex values for demonstration
import random
array_file1 = [f"{random.randint(0, 255):02X}" for _ in range(20)]
array_file2 = [f"{random.randint(0, 255):02X}" for _ in range(20)]

array_result = []
for i in range(0, len(array_file1)):
    if array_file1[i] != array_file2[i]:
        if i % 4 == 0:
            array_result.append(f"{array_file1[i]} {array_file1[i+1]} {array_file1[i+2]} {array_file1[i+3]}\t{array_file2[i]} {array_file2[i+1]} {array_file2[i+2]} {array_file2[i+3]}")
            i += 4
        elif i % 4 == 1:
            array_result.append(f"{array_file1[i-1]} {array_file1[i]} {array_file1[i+1]} {array_file1[i+2]}\t{array_file2[i-1]} {array_file2[i]} {array_file2[i+1]} {array_file2[i+2]}")
            i += 3
        elif i % 4 == 2:
            array_result.append(f"{array_file1[i-2]} {array_file1[i-1]} {array_file1[i]} {array_file1[i+1]}\t{array_file2[i-2]} {array_file2[i-1]} {array_file2[i]} {array_file2[i+1]}")
            i += 2
        elif i % 4 == 3:
            array_result.append(f"{array_file1[i-3]} {array_file1[i-2]} {array_file1[i-1]} {array_file1[i]}\t{array_file2[i-3]} {array_file2[i-2]} {array_file2[i-1]} {array_file2[i]}")
            i += 1

with open("Result.txt", "w") as file:
    file.write("\n".join(array_result))

Thank You

let me try first and look carefully that for loop.
 
Joined
Sep 21, 2022
Messages
196
Reaction score
26
Was your original intention to compare 2 binary files and output the difference to a text file?

If so, you're way off course with this regex stuff and strange %4 indexing.

Read a byte from each input file, compare it, if it is different, then convert the byte to a hex string and write it to the output text file. Repeat.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,073
Messages
2,570,539
Members
47,195
Latest member
RedaMahuri

Latest Threads

Top