bin

1.BIN2CSV:

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# Filename: bin2csv.py

# Version: 1.0.1

# Author: Jeoi Reqi


"""

Description:

This script converts a binary file (.bin) to a CSV file (.csv).

It assumes that each line in the binary file represents a separate record, & it decodes each line from UTF-8 before writing it to the CSV file.


Requirements:

- Python 3.x

- No additional external libraries are required.


Usage:

1. Save this script as 'bin2csv.py'.

2. Ensure your binary file ('example.bin') is in the same directory as the script.

3. Run the script.

4. The converted CSV file ('bin2csv.csv') will be generated in the same directory.


Note: Adjust the 'bin_filename' and 'csv_filename' variables in the script as needed.

"""


import csv


def bin_to_csv(bin_filename, csv_filename):

    with open(bin_filename, 'rb') as binfile, open(csv_filename, 'w', newline='') as csvfile:

        csvwriter = csv.writer(csvfile)

        for line in binfile:

            # Assuming each line in the binary file is a separate record

            csvwriter.writerow([line.decode('utf-8').strip()])


if __name__ == "__main__":

    bin_filename = 'example.bin'

    csv_filename = 'bin2csv.csv'

    bin_to_csv(bin_filename, csv_filename)

    print(f"Converted '{bin_filename}' to '{csv_filename}'.")



2.BIN2HTML:

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# Filename: bin2html.py

# Version: 1.0.1

# Author: Jeoi Reqi


"""

Description:

This script converts a binary file (.bin) to an HTML file (.html).

It embeds the binary content within a basic HTML structure, assuming each line in the binary file represents a separate record.

The script decodes each line from UTF-8 before embedding it into the HTML file.


Requirements:

- Python 3.x


Usage:

1. Save this script as 'bin2html.py'.

2. Ensure your binary file ('example.bin') is in the same directory as the script.

3. Run the script.

4. The converted HTML file ('bin2html.html') will be generated in the same directory.


Note: Adjust the 'bin_filename' and 'html_filename' variables in the script as needed.

"""


def bin_to_html(bin_filename, html_filename):

 with open(bin_filename, 'rb') as binfile, open(html_filename, 'w') as htmlfile:

     # Use basic HTML structure and write binary content

     htmlfile.write(f'<html><body><p>{"".join(line.decode("utf-8") for line in binfile)}</p></body></html>')


if __name__ == "__main__":

 bin_filename = 'example.bin'

 html_filename = 'bin2html.html'

 bin_to_html(bin_filename, html_filename)

 print(f"Converted '{bin_filename}' to '{html_filename}'.")



3.BIN2JSON:

#!/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}'.")



4.BIN2PDF:

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# Filename: bin2pdf.py

# Version: 1.0.0

# Author: Jeoi Reqi


"""

Description:

This script converts a binary file (.bin) to a PDF file (.pdf).

It reads the binary data from the file and converts it to a string representation.

The binary string is then written to a PDF file with each binary digit in a separate cell.


Requirements:

- Python 3.x

- FPDF library (install using: pip install fpdf)


Usage:

1. Save this script as 'bin2pdf.py'.

2. Ensure your binary file ('example.bin') is in the same directory as the script.

3. Install the FPDF library using the command: 'pip install fpdf'

4. Run the script.

5. The converted PDF file ('bin2pdf.pdf') will be generated in the same directory.


Note: Adjust the 'bin_filename' and 'pdf_filename' variables in the script as needed.

"""


from fpdf import FPDF


def bin_to_pdf(bin_filename, pdf_filename):

    with open(bin_filename, 'rb') as binfile:

        binary_data = binfile.read()


    pdf = FPDF()

    pdf.add_page()

    pdf.set_font("Arial", size=12)

    

    # Convert binary data to a string representation

    binary_string = ''.join(format(byte, '08b') for byte in binary_data)

    

    # Write binary string to PDF

    pdf.multi_cell(0, 10, binary_string)


    pdf.output(pdf_filename)


if __name__ == "__main__":

    # Set the filenames for the binary and PDF files

    bin_filename = 'example.bin'

    pdf_filename = 'bin2pdf.pdf'


    # Convert the binary file to a PDF file

    bin_to_pdf(bin_filename, pdf_filename)


    print(f"Converted '{bin_filename}' to '{pdf_filename}'.")



5.BIN2TXT:

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# Filename: bin2txt.py

# Version: 1.0.0

# Author: Jeoi Reqi


"""

Description:

This script converts a binary file (.bin) to a text file (.txt).

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 text file.


Requirements:

- Python 3.x


Usage:

1. Save this script as 'bin2txt.py'.

2. Ensure your binary file ('example.bin') is in the same directory as the script.

3. Run the script.

4. The converted text file ('bin2txt.txt') will be generated in the same directory.


Note: Adjust the 'bin_filename' and 'txt_filename' variables in the script as needed.

"""


def bin_to_txt(bin_filename, txt_filename):

 with open(bin_filename, 'rb') as binfile, open(txt_filename, 'w') as txtfile:

     for line in binfile:

         txtfile.write(line.decode('utf-8'))


if __name__ == "__main__":

 bin_filename = 'example.bin'

 txt_filename = 'bin2txt.txt'

 bin_to_txt(bin_filename, txt_filename)

 print(f"Converted '{bin_filename}' to '{txt_filename}'.")



6.BIN2XML:

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# Filename: bin2xml.py

# Version: 1.0.0

# Author: Jeoi Reqi


"""

Description:

This script converts a binary file (.bin) to an XML file (.xml).

It reads the binary content, assuming each line in the binary file represents a separate element.

The script decodes each line from UTF-8 and creates an XML file with elements under the root.


Requirements:

- Python 3.x


Usage:

1. Save this script as 'bin2xml.py'.

2. Ensure your binary file ('example.bin') is in the same directory as the script.

3. Run the script.

4. The converted XML file ('bin2xml.xml') will be generated in the same directory.


Note: Adjust the 'bin_filename' and 'xml_filename' variables in the script as needed.

"""


import xml.etree.ElementTree as ET


def bin_to_xml(bin_filename, xml_filename):

 root = ET.Element("root")

 with open(bin_filename, 'rb') as binfile:

     for line in binfile:

         # Assuming each line in the binary file is a separate element

         ET.SubElement(root, "element").text = line.decode('utf-8').strip()


 tree = ET.ElementTree(root)

 tree.write(xml_filename)


if __name__ == "__main__":

 bin_filename = 'example.bin'

 xml_filename = 'bin2xml.xml'

 bin_to_xml(bin_filename, xml_filename)

 print(f"Converted '{bin_filename}' to '{xml_filename}'.")