Hide

Problem G
XOR

Write a program that:

  1. Prompts the user for a file name.

  2. 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".

  3. Calculates an XOR checksum by performing byte-wise XOR operations for every byte in the file.

  4. 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:

  1. 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

Please log in to submit a solution to this problem

Log in