forked from pulp-platform/occamy
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
125 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import argparse | ||
import os | ||
|
||
# Convert a binary file to a hex string and save it to a text file | ||
def binary_to_hex(input_binary_file, output_text_file, chunk_size): | ||
try: | ||
# Open the binary file in read-binary mode | ||
with open(input_binary_file, 'rb') as bin_file: | ||
binary_data = bin_file.read() # Read the entire binary file | ||
|
||
# Initialize a list to hold the lines | ||
lines = [] | ||
|
||
total_length = len(binary_data) | ||
index = 0 | ||
|
||
# Process the data in chunks of 64 bytes | ||
while index < total_length: | ||
chunk = binary_data[index:index + chunk_size] | ||
|
||
# Reverse the data in the chunk (MSB to LSB within the line) | ||
reversed_chunk = chunk[::-1] | ||
|
||
# Convert the reversed chunk to a hexadecimal string | ||
hex_chunk = reversed_chunk.hex() | ||
|
||
# For the last chunk, if it's less than 64 bytes, pad zeros at the end | ||
if len(chunk) < chunk_size: | ||
pad_length = chunk_size - len(chunk) | ||
# Pad with zeros at the end to make up to 64 bytes (128 hex digits) | ||
hex_chunk = '00' * pad_length + hex_chunk | ||
|
||
# Add the hex_chunk to the list of lines | ||
lines.append(hex_chunk) | ||
index += chunk_size | ||
|
||
# Write the lines to the output text file | ||
with open(output_text_file, 'w') as text_file: | ||
for line in lines: | ||
text_file.write(line + '\n') | ||
|
||
print(f"Hex data has been written to {output_text_file}") | ||
|
||
except FileNotFoundError: | ||
print(f"Error: File {input_binary_file} not found.") | ||
except Exception as e: | ||
print(f"An error occurred: {e}") | ||
|
||
def convert_all_bin_to_hex(directory): | ||
# Traverse the directory | ||
for root, _, files in os.walk(directory): | ||
for file in files: | ||
if file.endswith('.bin'): | ||
input_file = os.path.join(root, file) | ||
output_file = f"{input_file.rsplit('.', 1)[0]}.hex" | ||
binary_to_hex(input_file, output_file, chunk_size=64) | ||
|
||
def main(): | ||
# Set up argument parser | ||
parser = argparse.ArgumentParser(description="Convert all binaries in strings and save it to a text file.") | ||
parser.add_argument( | ||
'-i', '--input', | ||
type=str, | ||
default='.', | ||
help="Path to the folder of binary files. Defaults to the current folder." | ||
) | ||
# Parse arguments | ||
args = parser.parse_args() | ||
# Execute the task | ||
convert_all_bin_to_hex(args.input) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters