Converting data to binary

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!
lines 1-3 should be fairly obvious.

line 5, iterate through the entire string.

line 7 get the character at the current location

line 8 iterate through all 8 bits

line 12

1
2
3
4
5
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 
THANKS A TON! I got the code to compile and run!


Now, would this be the time to buffer the string by 4 (that is what my professor said we had to do, not sure when I would do this)

Also, would I buffer it by adding 4 '0's using a loop?


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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#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 << "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); 	
	                 }
                   }
            }
        else if (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;
            }
        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() 


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!
Last edited on
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.
Topic archived. No new replies allowed.