Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have included the binary file below I need to convert it to strings in a .txt

ID: 3866233 • Letter: I

Question

I have included the binary file below I need to convert it to strings in a .txt file using Python. The implementation must use the struct module and the .unpack() function. Here is the binary code:

010000100110000101110010011100100110111101110111001011010110100101101110001011010100011001110101011100100110111001100101011100110111001100111010010101100110100101100011011000010111001001110011011101000110111101110111011011100011101000110001
010101100110100101100011011000010111001001110011011101000110111101110111011011100011101001000010011000010110110001101100011000010110100001101111011011110011101000110101
01010110011010010110001101100001011100100111001101110100011011110111011101101110001110100100001101110010011011110111011001100001011011100010011101110011
010001110110000101110100011001010011101000110111
01000010011000010110110001101100011000010110100001101111011011110011101001001110011011110111001001110010011000010110110101100010011110010011101000110010
0100001001100001011011000110110001100001011010000110111101101111001110100100001101110010011011110111011001100001011011100010011101110011
010001110110000101110100011001010011101000110010
0100001101110010011011110111011001100001011011100010011101110011
0100011101100001011101000110010100111010010000110111001001101111011100110010110101101110011110010010110101000011011101010110100101110010011011100011101000110010
0100001101110010011011110111011001100001011011100010011101110011
01000111011000010111010001100101001110100100101101100101011011000110110001110011011101000110100001101111011100100111000001100101
010100100110111101100001011001000011101000110100
010000110111001001101111011100110010110101101110011110010010110101000011011101010110100101110010011011100011101001000111011011000110010101101110011011100110111101100011011010110011101000110011
010001110110110001100101011011100110111001101111011000110110101100111010010100100110100001100101011011100110010101100001011100110011101000110011
010100100110100001100101011011100110010101100001011100110011101001010011011010110110000101110010011011000110111101100101011110010011101000110001
01010011011010110110000101110010011011000110111101100101011110010011101001001100011000010110101101100101011100110110100101100100011001010011101000110001
010011000110000101101011011001010111001101101001011001000110010100111010010100100110100001100101011011100110010101100001011100110011101000110001
0100101101100101011011000110110001110011011101000110100001101111011100100111000001100101
0101001001101111011000010110010000111010010100100110111101101100011001100010011101110011
0100001101100001011100110111010001101100011001010011101000110010
010100100110111101101100011001100010011101110011
0100001101100001011100110111010001101100011001010011101001001011011010010111001001101011
01010010011011110110111001100001011011100011101000110010
0100101101100101011011000110110001110011011101000110100001101111011100100111000001100101
0101001001101111011000010110010000111010010010110110100101101100011001000110000101101110011001010011101000110111
010010110110100101101100011001000110000101101110011001010011101001000001011000100110001001100101011110010011101000110010
01000001011000100110001001100101011110010011101001001011011010010111001001101011
0100110101100001011000110110100001100001011011100011101000110100
01001011011010010111001001101011
010011010110000101100011011010000110000101101110001110100101001101101000011010010110110001101111011010000011101000110001
010100110110100001101001011011000110111101101000001110100101001101101011011000010111001001101100011011110110010101111001
010100100110111101100001011001000011101000110010
0101001101101011011000010111001001101100011011110110010101111001
010100100110111101100001011001000011101001000100011001010111011001101001011011000010011101110011
010000100110000101100011011010110011101000110001
01000100011001010111011001101001011011000010011101110011
0100001001100001011000110110101100111010010000110111010101101100011001000110010101100101
01000110011001010110110001101100
0101001101110101011011010110110101101001011101000011101000110001
01001011011010010111001001101011
0100110101100001011000110110100001100001011011100011101001010000011001010110010101101100
0100011101101111011001000111001001100101011001000011101000110010
010010110110100101101100011001000110000101101110011001010011101001000011011100100110111101101110011010110011101000110001
01000011011100100110111101101110011010110011101001001101011000010111001001101111011011100011101000110010
010011010110000101110010011011110110111000111010010101110110010101101100011011000111001101110111011011110111001001110100011010000011101000110100
0101011101100101011011000110110001110011011101110110111101110010011101000110100000111010010100110111010101100100011001000110010101110010011110010011101000110011
0101001101110101011001000110010001100101011100100111100100111010010000100111001001100101011011100110010001100001011011010011101000110100
01010111011001010110110001101100011100110111011101101111011100100111010001101000001110100100001101110010011011110111001101100010011110010011101000110010
0100001101110010011011110111001101100010011110010011101001001011011011100110000101110000011001100110111101110010011001000011101000110100
01001011011011100110000101110000011001100110111101110010011001000011101001000100011100100111100101100001011101110011101000110010
01000100011100100111100101100001011101110011101001010100011011110111001001111001011100100110010101100011011010110011101000110001
0101010001101111011100100111100101110010011001010110001101101011001110100100010101101100011100110110001001110010011010010110010001100111011001010011101000110010
01000101011011000111001101100010011100100110100101100100011001110110010100111010010010000110000101100011011010110110010101101110011000100110010101100011011010110011101000110011
01001000011000010110001101101011011001010110111001100010011001010110001101101011001110100100011001100110011000010111001001110001011101010110100001100001011100100011101000110001
01001011011011100110000101110000011001100110111101110010011001000011101001010100011010010110010001101101011011110111010101110100011010000011101000110001
010101000110100101100100011011010110111101110101011101000110100000111010010010000110000101110101011011000111010001110010011000010111010101100111011010000011101000110011
010010000110000101110101011011000111010001110010011000010111010101100111011010000011101001000001011100100110110001100101011100110110001001110101011100100110011101101000
010101110110010101110011011101000011101000110010
01000001011100100110110001100101011100110110001001110101011100100110011101101000
010101110110010101110011011101000011101001000001011100100110110001100101011100110110001001110101011100100110011101101000
010000100111001001101001011001000110011101100101
0101001101110100001011100011101000110001
01000001011100100110110001100101011100110110001001110101011100100110011101101000
010000100111001001101001011001000110011101100101
010100110111010000101110001110100100011001100110011000010111000101110101011010000110000101110010
010100100110111101100001011001000011101000110001
0100011001100110011000010111000101110101011010000110000101110010
0101001001101111011000010110010000111010010011010110000101110010011101000110100001110111011000010110100101110100011001010011101000110001
0100110101100001011100100111010001101000011101110110000101101001011101000110010100111010010000010111001001101100011001010111001101100100011000010110110001100101
01000111011100100110010101100101011011100011101000110001
010000010111001001101100011001010111001101100100011000010110110001100101
0100011101110010011001010110010101101110001110100100000101110010011011000110010101110011011001000110000101101100011001010011101000110001
0100011101110010011001010110010101101110001110100100000101110010011011000110010101110011011001000110000101101100011001010011101000110001

Explanation / Answer

The Bytes type is a binary data type, it just stores a bunch of 8bit characters. Note that the code with struct.
pack ends up creating a bytes object:

import struct
data = struct.pack("24s",b"This is an unknown string")
type(data)
<class 'bytes'>
len(data)
24
The length of this is 24 as per your format specifier. If you just want to place the bytes-string directly
into the file without doing any length checking you don't even need to use the struct module,
you can just write it directly to the file:

F = open("data.bin", "wb")
F.write(b"This will work")
If however you wanted to keep the 24 bytes length you could keep using struct.pack:

>>> data = struct.pack("24s",b"This is an unknown st")
>>> len(data)
24
>>> print(data)
b'This is an unknown st'
>>> data = struct.pack("24s",b"This is an unknown string abcdef")
>>> print(data)
b'This is an unknown strin'
In the case of supplying a bytes that is too short struct.pack pads the remainder with 0's and
in the case where it's too long it truncates it.

If you don't mind getting the missing space padded out with zeros you can just pass in the bytes
object directly to struct.