Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: bin2json.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a binary file (.bin) to a JSON file (.json).
- It reads the binary content, assuming each line in the binary file represents a separate record.
- The script decodes each line from UTF-8 and saves the data as a JSON file with a "binary_content" key.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'bin2json.py'.
- 2. Ensure your binary file ('example.bin') is in the same directory as the script.
- 3. Run the script.
- 4. The converted JSON file ('bin2json.json') will be generated in the same directory.
- Note: Adjust the 'bin_filename' and 'json_filename' variables in the script as needed.
- """
- import json
- def bin_to_json(bin_filename, json_filename):
- data = {"binary_content": []}
- with open(bin_filename, 'rb') as binfile:
- for line in binfile:
- data["binary_content"].append(line.decode('utf-8').strip())
- with open(json_filename, 'w') as jsonfile:
- json.dump(data, jsonfile, indent=2)
- if __name__ == "__main__":
- bin_filename = 'example.bin'
- json_filename = 'bin2json.json'
- bin_to_json(bin_filename, json_filename)
- print(f"Converted '{bin_filename}' to '{json_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement