Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <math.h>
- using namespace std;
- unsigned char convertedChar[12];
- char firstChar;
- char secondChar;
- unsigned char parityChar;
- /*this is used to print out the bits of any character that I think is failing so i can hand check it.
- */
- void bitting(unsigned char t){
- for(int b = 0; b < 8; t >>=1){
- if(t & 01)
- cout << "1";
- else
- cout << "0";
- b++;
- }
- cout << endl;
- }
- /*This converts the char that is brought in into a 12 character array.
- *This makes it much easier to check the parity of the object and then to stick it back into the char
- */
- void convert(unsigned char input){
- for( int b = 0; b < 12; b++){
- if(b == 0 || b == 1 || b == 3 || b == 7) {
- convertedChar[b]= '0';
- }else if(input & 01){
- convertedChar[b] = '1';
- input >>=1;
- }else{
- convertedChar[b] ='0';
- input >>=1;
- }
- }
- }
- /*This takes in the number of what parity to check.
- *It then does that is required. 1 skip 1 or 2 skip 2... and so on
- */
- bool parityCheck(int a){
- int count = a;
- bool temp = false;
- for (int i = a-1; i < 12; i+=1){
- if(convertedChar[i] == '1')
- temp = !temp;
- count--;
- if(count == 0){
- count = a;
- i+=a;
- }
- }
- return temp;
- }
- /*Writes the parity to whatever the character is */
- void writeParity(unsigned char input){
- convert(input);
- int multi = 0;
- for(int count = 3; count >=0; count--){
- if(parityCheck(pow(2,count)))
- multi+=pow(2,count);
- parityChar = parityChar | multi;
- }
- }
- /* reads in the first two chars computes and then starts writing out. */
- int main() {
- bool running = true;
- while(running){
- firstChar = firstChar & 0x00;
- secondChar = secondChar & 0x00;
- cin.get(firstChar);
- cin.get(secondChar);
- parityChar = parityChar & 0x00;
- if(secondChar)
- writeParity(secondChar);
- else
- running = false;
- parityChar <<=4;
- if(firstChar)
- writeParity(firstChar);
- else
- running = false;
- if(running)
- cout << firstChar << parityChar << secondChar;
- if(!running && firstChar)
- cout << firstChar << parityChar;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment