File size: 722 Bytes
8f0cb79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


def file_to_binary(filepath):
    """

    Reads a file in binary mode and converts its content into a binary (0s and 1s) string.

    

    Args:

        filepath (str): The path to the file.

    

    Returns:

        str: A string representation of the file's binary content.

    """
    try:
        with open(filepath, 'rb') as file:
            binary_data = file.read()
            # Convert bytes to binary string
            binary_string = ''.join(format(byte, '08b') for byte in binary_data)
        return binary_string
    except FileNotFoundError:
        print("Error: File not found.")
        return None
    except Exception as e:
        print(f"Error: {e}")
        return None