Advertisement
Python253

decode_0bin_1bin_files

Mar 5th, 2024
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: decode_0bin_1bin_files.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. This Python script decodes the contents of '0.bin' and '1.bin' files, saving the results as plain text in corresponding txt files.
  8.  
  9. Requirements:
  10. - Python 3
  11.  
  12. Description:
  13. The script takes two binary files, 0.bin and 1.bin, as input and decodes their contents into ASCII plaintext using UTF-8 encoding.
  14. The decoded text is then saved to two separate text files, decoded_0.txt and decoded_1.txt, respectively.
  15. The purpose of this script is to demonstrate the decoding process and verify the functionality of decoding binary files into human-readable text.
  16.  
  17. Usage:
  18. Run the script using a Python 3 interpreter. Ensure that the 0.bin and 1.bin files are present in the same directory as the script.
  19. The decoded text will be saved in decoded_0.txt and decoded_1.txt.
  20. """
  21.  
  22. import os
  23.  
  24. def decode_binary_file(input_file, output_file):
  25.     with open(input_file, 'rb') as binary_file:
  26.         # Read binary data
  27.         binary_data = binary_file.read()
  28.  
  29.         # Decode binary data into ASCII plaintext
  30.         decoded_text = binary_data.decode('utf-8')
  31.  
  32.         # Write the decoded text to the output file
  33.         with open(output_file, 'w') as plaintext_file:
  34.             plaintext_file.write(decoded_text)
  35.  
  36. if __name__ == "__main__":
  37.     # Specify the input and output file names
  38.     input_file_0 = '0.bin'
  39.     output_file_0 = 'decoded_0.txt'
  40.  
  41.     input_file_1 = '1.bin'
  42.     output_file_1 = 'decoded_1.txt'
  43.  
  44.     # Decode 0.bin
  45.     decode_binary_file(input_file_0, output_file_0)
  46.     print(f'{input_file_0} decoded and saved to {output_file_0}')
  47.  
  48.     # Decode 1.bin
  49.     decode_binary_file(input_file_1, output_file_1)
  50.     print(f'{input_file_1} decoded and saved to {output_file_1}')
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement