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

Write a program that calculates the CRC-16 of a given file and appends it to the

ID: 3559092 • Letter: W

Question

Write a program that calculates the CRC-16 of a given file and appends it to the end of the same file. Your program must also be able to verify the correctness of a given file that already has CRC appended at the end. Use the CRC polynomial x^16 + x^10 + x^8 + x^7 + x^3 + 1.

Input format:

Your program should prompt the user for the following input:

Enter the name of the file you want to check:

-------------Menu-------------

Calculate CRC

Verify CRC

Exit

Choose from the above menu:

You must check for input correctness also, by implementing code to:

Verify that user has input a valid option in the menu. If invalid, prompt for menu again.

Verify that the file that the user has entered actually exists. If the file does not exist, prompt for file again.

Verify that each character in the input file is a valid hexadecimal character
0,1,2,3,4,5,6,7,8,9A,B,C,D,E,F

Output format:

The input file will contain data in hexadecimal character. Print out the hexadecimal file that is read.

Next, you must convert the hexadecimal file into binary. Recall that one hex character is four bits. Read the input file and form a binary string. The length of the binary string will be four times the length of the input file because each hexadecimal character will be replaced by 4 binary bits. You will have to define a separate function to get the binary equivalent of a hexadecimal character.

You must print the binary version of the input file 32 binary bits per line. You must also insert a space after every four binary bits that you print.

Next, you will show the step-by-step of the CRC calculation or verification.

In the end, when calculating CRC, you must show the CRC answer, then convert it to hexadecimal and append it to the end of the file and close the file.

On the other hand, if verifying CRC, you must print a message whether the CRC check passed or failed.

Specific Functions inside code:

You must implement the following functions:

A function for converting hexadecimal string into binary string

A function for converting binary to hexadecimal

A function to validate the input

An XOR function that takes as input two binary strings and returns the XOR result.

A function for CRC calculation

A function for CRC verification

Enter the name of the file you want to check:

-------------Menu-------------

  1. Calculate CRC

  2. Verify CRC

  3. Exit

Choose from the above menu:

Explanation / Answer

Program:


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class CRC16Calculator
{
   public static void main(String args[]) throws IOException
   {
       Scanner readInput=new Scanner(System.in);
       String divisor="10000010110001001";
       System.out.println("Enter the name of the file you want to check:");
       String fileName=readInput.nextLine();
       File file=new File(fileName);
       //Read the data from file
       while(!file.exists())
       {
           System.out.println("File does not exist");
           System.out.println("Enter the name of the file you want to check:");
           fileName=readInput.nextLine();
           file=new File(fileName);
       }
       //Input validation
       Scanner readFile=new Scanner(file);
       String hexaString="";
       System.out.println("The input file(hex): ");
       while(readFile.hasNextLine())
       {
           hexaString=readFile.nextLine();
           System.out.println(hexaString);
       }
       readFile.close();
       readFile=new Scanner(file);
       System.out.println("The input file(bin):");
       //Convert hexadecimal values to bianry values
       hexaString="";
       String binaryString="";
       int byteCount=0;
       while(readFile.hasNextLine())
       {
           hexaString=readFile.nextLine();
           int len=hexaString.length();
           for(int i=0;i<len;i++)
           {
               String ch=hexaString.substring(0,1);
               hexaString=hexaString.substring(1,hexaString.length());
               //Convert the hexaString into binary
               String binStr=hexaToBinary(ch);  
              
               byteCount++;
               if((byteCount%8)==0)
                   System.out.println(binStr);
               else
                   System.out.print(binStr+" ");
               binaryString=binaryString+binStr;
          
           }
       }
       //Menu
       while(true)
       {
           System.out.println(" -------------Menu-------------");
           System.out.println("1.Calculate CRC");
           System.out.println("2.Verify CRC");
           System.out.println("3.Exit");
           System.out.println("Choose from the above menu: ");
           int choice=readInput.nextInt();
           switch(choice)
           {
           case 1:
              
               if(binaryString.length()<32)
               {
                   String append="";
                   for(int i=0;i<32-binaryString.length();i++)
                   {
                       append=append+"0";          
                   }
                   binaryString=binaryString+append;
                      
               }
              
               int n=binaryString.length()-divisor.length()+1;
               //CRC calculation
               for(int i=0;i<n;i++)
               {
                   String s1=binaryString.substring(0,i);
                   String s2=binaryString.substring(i,i+divisor.length());
                   String s3=binaryString.substring(i+divisor.length(),binaryString.length());
                   String xorResult="";
                   //apply XOR
                   if(s2.charAt(0)!='0')
                   {
                       xorResult=crcCalculation(s2,divisor);  
                       binaryString=s1+xorResult+s3;
                       boolean remainderZero=true;
                       for(int j=0;j<xorResult.length();j++)
                           if(xorResult.charAt(j)=='1')
                               remainderZero=false;
                       if(remainderZero==true)
                           break;
                   }
                   //Print binary string in each step
                   for(int k=0;k<binaryString.length();k=k+4)
                   {
                       System.out.print(binaryString.substring(k,k+4)+" ");
                   }
                   System.out.println();
               }
               //Find CRC from the remainder
               int x=binaryString.indexOf("1");
               String rem=binaryString.substring(x,binaryString.length());
               int len=(int)(rem.length()/4);
               if((rem.length()%4)!=0)
               {
                   String append1="";
                   for(int i=0;i<(len+1)*4-rem.length();i++)
                   {
                       append1=append1+"0";
                   }
               rem=rem+append1;
               }
               String hexa="";
               //Convert the CRC into Hexa decimal values
               for(int i=0;i<rem.length();i=i+4)
               {
                   String sub=rem.substring(i,i+4);
                   String hexStr=binaryTohexa(sub);
                   hexa=hexa+hexStr;
               }
               //print hexa CRC value
               System.out.println("Hexa: "+hexa);
               //Append the CRC to the data file
               FileWriter fw = new FileWriter("data.txt", true);
               BufferedWriter bw = new BufferedWriter(fw);
               bw.write(hexa);
               bw.close();
               fw.close();
               break;
           case 2: if(crcVerification())
                       System.out.println("CRC check passed");
                   else
                       System.out.println("CRC check failed");
                   break;
           case 3: System.exit(0);break;
           default: System.out.println("Wrong choice");
           }
       }
   }
   public static String hexaToBinary(String hex)
   {
       String bin="";
       for(int i=0;i<hex.length();i++)
       {
           if(hex.charAt(i)=='0')
           {
               bin=bin+"0000";
           }
           else if(hex.charAt(i)=='1')
           {
               bin=bin+"0001";
           }
           else if(hex.charAt(i)=='2')
           {
               bin=bin+"0010";
           }
           else if(hex.charAt(i)=='3')
           {
               bin=bin+"0011";
           }
           else if(hex.charAt(i)=='4')
           {
               bin=bin+"0100";
           }
           else if(hex.charAt(i)=='5')
           {
               bin=bin+"0101";
           }
           else if(hex.charAt(i)=='6')
           {
               bin=bin+"0110";
           }
           else if(hex.charAt(i)=='7')
           {
               bin=bin+"0111";
           }
           else if(hex.charAt(i)=='8')
           {
               bin=bin+"1000";
           }
           else if(hex.charAt(i)=='9')
           {
               bin=bin+"1001";
           }
           else if(hex.charAt(i)=='A')
           {
               bin=bin+"1010";
           }
           else if(hex.charAt(i)=='B')
           {
               bin=bin+"1011";
           }
           else if(hex.charAt(i)=='C')
           {
               bin=bin+"1100";
           }
           else if(hex.charAt(i)=='D')
           {
               bin=bin+"1101";
           }
           else if(hex.charAt(i)=='E')
           {
               bin=bin+"1110";
           }
           else if(hex.charAt(i)=='F')
           {
               bin=bin+"1111";
           }
          
       }
       return bin;
   }
   public static String binaryTohexa(String bin)
   {
       String hex="";
       int count=0;
       int hexaValue=0;
       for(int i=0;i<bin.length();i++)
       {
           if(bin.charAt(i)=='0')
           {
              
               count++;
           }
           else if(bin.charAt(i)=='1')
           {   count++;
               hexaValue=(int) (hexaValue+Math.pow(2, 4-count));
              
           }
           if(count==4)
           {
               if(hexaValue>9)
               {
                   if(hexaValue==10)
                       hex=hex+"A";
                   else if(hexaValue==11)
                       hex=hex+"B";
                   else if(hexaValue==12)
                       hex=hex+"C";
                   else if(hexaValue==13)
                       hex=hex+"D";
                   else if(hexaValue==14)
                       hex=hex+"E";
                   else if(hexaValue==15)
                       hex=hex+"F";
               }
               else
               {
                   hex=hex+hexaValue;
               }
               hexaValue=0;
               count=0;
           }
       }
       return hex;
   }
   public static String XOR(String s1,String s2)
   {
       String result="";
       for(int i=0;i<s1.length();i++)
       {
           if(s1.charAt(i)==s2.charAt(i))
           {
               if(s1.charAt(i)!=' '&&s1.charAt(i)!=' ')
                   result+="0";
           }
           else
               result+="1";
       }
       return result;
   }
   public static String crcCalculation(String Message, String divisor)
   {
       String result="";
       for(int i=0;i<Message.length();i++)
       {
           if(Message.charAt(i)!=divisor.charAt(i))
               result=result+"1";
           else
               result=result+"0";
       }
       return result;
   }
   public static boolean crcVerification()
   {
       return false;
   }
}

Sample output:

Enter the name of the file you want to check:
data.txt
The input file(hex):
AB1245
The input file(bin):
1010 1011 0001 0010 0100 0101
-------------Menu-------------
1.Calculate CRC
2.Verify CRC
3.Exit
Choose from the above menu:
1
0010 1001 1101 0110 1100 0101 0000 0000
0010 1001 1101 0110 1100 0101 0000 0000
0000 1001 0110 0111 1110 0101 0000 0000
0000 1001 0110 0111 1110 0101 0000 0000
0000 0001 0100 1011 1010 1101 0000 0000
0000 0001 0100 1011 1010 1101 0000 0000
0000 0001 0100 1011 1010 1101 0000 0000
0000 0000 0100 1110 0010 0100 0000 0000
0000 0000 0100 1110 0010 0100 0000 0000
0000 0000 0000 1111 0100 0110 0100 0000
0000 0000 0000 1111 0100 0110 0100 0000
0000 0000 0000 1111 0100 0110 0100 0000
0000 0000 0000 0111 0110 1010 0000 1000
0000 0000 0000 0011 0111 1100 0010 1100
0000 0000 0000 0001 0111 0111 0011 1110
0000 0000 0000 0000 0111 0010 1011 0111
Hexa: E56E
CRC check failed

-------------Menu-------------
1.Calculate CRC
2.Verify CRC
3.Exit
Choose from the above menu:
3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote