CRC algorithm

I am looking for some working source code for computing the CRC of a string. I have been researching how to do it the last 2 weeks, and I am still LOST! I was hoping that someone would be able to explain to me how the code works in hopes that I could use it within my CRC program.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#define P 10010
#define SignatureBlock "~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\nNicholas Whitmore\nnicholas.whitmore@maine.edu\nCIS 215, C++ Application Programming\n~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\n"
using namespace std;
using std::string;



int main()
{
    cout << SignatureBlock << endl;//implement signature block
    
    cout << "" << endl;
    cout << "CRC Check Program\n\n" << endl;//title
    
    cout << "Would you like to check a file or a string?\n" << endl; //prompt for user input
    cout << "1) To Check a File\n" << endl;//option 1
    cout << "2) To Check a String\n" << endl;//option 2
    cout << "3) To exit\n" << endl;//option 3
    
    int select; //user selection variable
    cin >> select;//user input
        if (select == 1)//user chose option 1
            {
                cout << "\nEnter the name of the file including path:\n" << endl;//prompt for file path
	            string filePath; //file path variable
                cin.ignore();
	            getline(cin, filePath); //user input
                cout << "File mode:\n"<<endl;//file mode title
                ifstream myfile(filePath.c_str());//opens file at filePath
                string line;
                if (myfile.is_open())//check file path
                {
                   while (getline(myfile,line))
                   {
                         cout<<"Data is: "<< line << endl;//output the data within the file
                   }
                   cout<<"Read 31 bytes from " << filePath<<endl;
                   cout<<"Check byte is: " <<endl;
                   myfile.close();//close the file
                }
                else cout << "Unable to open file" << endl;//if file path is incorrect
                
                vector<bool> current_bits; // vector used so the size can be changed
                int i,j; 
                char current_byte; // byte pointer
                	  
                   for(i=0; i<line.length();++i) // iterate through the entire string
                   {
	                 current_byte = line.at(i); // get the character at the current location
	                 for(j=7;j>=0;--j)  // iterate through all 8 bits
	                 {
	                	  current_bits.push_back((current_byte & (1<<j)) != 0); 	
	                 }
                   }
            }
        else if (select == 2)//user chose option 2
            {
                cout << "\nEnter the string to be checksummed:\n" << endl;//prompt for string
                string cString; //string to be checked variable
                cin.ignore();
                getline(cin, cString);//user input
                cout<<"Text mode:\n"<<endl;

                vector<bool> current_bits; // vector used so the size can be changed
                int i,j; 
                char current_byte; // byte pointer
                	  
                   for(i=0; i<cString.length();++i) // iterate through the entire string
                   {
	                 current_byte = cString.at(i); // get the character at the current location
	                 for(j=7;j>=0;--j)  // iterate through all 8 bits
	                 {
	                	  current_bits.push_back((current_byte & (1<<j)) != 0);	
	                 }
                   }
                cout<<"CRC of [" << cString << "] is [] with P= ["<< P <<"]\nHexadecimal checksum: "<< endl;
            }
        else if (select == 3)//user chose option 3
            {
            cout << "Thank you! Have a nice day!" << endl; //exits the program
            }
        else//user chose an invalid option
            {
            cout << "You have chosen an invalid option. Good bye." << endl;//invalid option. program closes
            }
    cin.get(); //keeps the command prompt window open
    return 0;
}//end main() 


I have been able to convert the string to binary successfully, just have no idea how to implement the polynomial division. I was planning on passing the vector<bool> current_bits; as a reference to a separate function to do the work.


ALSO: How would I use a hexadecimal checksum within the program?
Last edited on
I have 3 more days to write this program
I NEED HELP ASAP!!!!!!!!
Topic archived. No new replies allowed.