Problem G
XOR
Write a program that:
-
Prompts the user for a file name.
-
Opens the file in binary mode, using the "rb" mode of the open function. The files can be found in the directory "/src/".
-
If the file does not exist, then the program should print "No file named {file name} could be found".
-
-
Calculates an XOR checksum by performing byte-wise XOR operations for every byte in the file.
-
Prints out "The checksum is <checksum>" where <checksum> is a single byte in hexadecimal format (e.g. xB2).
Example: Let’s assume that the file a.bin contains three bytes:
-
0x01, 0x02 and 0x01.
Then the XOR checksum is:
-
0x01 ^ 0x02 ^ 0x01 = 0x02.
Input
Input consists of:
-
A filename containing the binary file to be processed.
Output
Output consists of either "No file named {file name} could be found" or "The checksum is <checksum>" where <checksum> is a single byte in hexadecimal format, such as xB2.
Sample Input 1 | Sample Output 1 |
---|---|
random.bin |
The checksum is x96 |
Sample Input 2 | Sample Output 2 |
---|---|
cancel.bin |
The checksum is x5b |
Sample Input 3 | Sample Output 3 |
---|---|
empty.bin |
The checksum is x00 |
Sample Input 4 | Sample Output 4 |
---|---|
doesnotexist.bin |
No file named doesnotexist.bin could be found |