I figured I would break my previous post into parts. Right now I am trying to write a CRC program. I am completely new to programming (having only written a hello world program prior to this one) I am trying to understand how to convert a user defined file into binary to apply the CRC checksum to it and get a result.
MY QUESTION:
How do I convert a string("Four score and seven years ago")into Binary?
Below is the sample code the professor has provided us, but I don't know how to pick it apart
1 2 3 4 5 6 7 8 9 10 11 12 13 14
vector<bool> current_bits; // use a vector so the array is re-sizable
int i,j; // Array indices
char current_byte; // byte being considered
for(i=0; i<datastring.length();++i) // traverse each byte in the data string
{
current_byte = datastring.at(i); // pick the byte we're working on
for(j=7;j>=0;--j) // for each bit in the current byte
{
// mask out the bit we want and append it to vector
current_bits.push_back((current_byte & (1<<j)) != 0);
}
}
Basically, I am looking for someone to dumb it down for me to understand. Thanks in advance!
current_bits.push_back(value) //add value to current_bits
current_byte & value //bitwise comparison of two numbers
//00000001 & 10101010 returns 00000000
1 << j //shift 00000001 j bits to the left
!= 0 //if that bit is a 1
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#define SignatureBlock "~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\nNicholas Whitmore\nnicholas.whitmore@maine.edu\nCIS 215, C++ Application Programming\n~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\n"
usingnamespace 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 << "Enter the name of the file including path:" << 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
cout<<"Read 31 bytes from " << filePath<<endl;
cout<<"Check byte is: " <<endl;
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
}
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);
}
}
}
elseif (select == 2)//user chose option 2
{
cout << "Enter the string to be checksummed:" << 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= []\nHexadecimal checksum: "<< endl;
}
elseif (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()
Did I implement this correctly?
I am sure that the code needs to be cleaned up. If you have suggestions on how to do it (because I haven't a clue on how to do it) PLEASE LET ME KNOW! I am eager to learn how this stuff works!
Not sure what he's asking for when he says buffer by four, but I can almost guarantee that it has nothing to do with the bits, as bytes require 8 bits.