Guest User

Untitled

a guest
Jun 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <math.h>
  4. using namespace std;
  5. unsigned char convertedChar[12];
  6. char firstChar;
  7. char secondChar;
  8. unsigned char parityChar;
  9.  
  10. /*this is used to print out the bits of any character that I think is failing so i can hand check it.
  11. */
  12. void bitting(unsigned char t){
  13. for(int b = 0; b < 8; t >>=1){
  14. if(t & 01)
  15. cout << "1";
  16. else
  17. cout << "0";
  18. b++;
  19. }
  20. cout << endl;
  21. }
  22.  
  23. /*This converts the char that is brought in into a 12 character array.
  24. *This makes it much easier to check the parity of the object and then to stick it back into the char
  25. */
  26. void convert(unsigned char input){
  27. for( int b = 0; b < 12; b++){
  28. if(b == 0 || b == 1 || b == 3 || b == 7) {
  29. convertedChar[b]= '0';
  30. }else if(input & 01){
  31. convertedChar[b] = '1';
  32. input >>=1;
  33. }else{
  34. convertedChar[b] ='0';
  35. input >>=1;
  36. }
  37. }
  38. }
  39. /*This takes in the number of what parity to check.
  40. *It then does that is required. 1 skip 1 or 2 skip 2... and so on
  41. */
  42. bool parityCheck(int a){
  43. int count = a;
  44. bool temp = false;
  45. for (int i = a-1; i < 12; i+=1){
  46. if(convertedChar[i] == '1')
  47. temp = !temp;
  48. count--;
  49. if(count == 0){
  50. count = a;
  51. i+=a;
  52. }
  53. }
  54. return temp;
  55. }
  56. /*Writes the parity to whatever the character is */
  57. void writeParity(unsigned char input){
  58. convert(input);
  59. int multi = 0;
  60. for(int count = 3; count >=0; count--){
  61. if(parityCheck(pow(2,count)))
  62. multi+=pow(2,count);
  63. parityChar = parityChar | multi;
  64. }
  65. }
  66. /* reads in the first two chars computes and then starts writing out. */
  67. int main() {
  68. bool running = true;
  69. while(running){
  70. firstChar = firstChar & 0x00;
  71. secondChar = secondChar & 0x00;
  72. cin.get(firstChar);
  73. cin.get(secondChar);
  74. parityChar = parityChar & 0x00;
  75. if(secondChar)
  76. writeParity(secondChar);
  77. else
  78. running = false;
  79. parityChar <<=4;
  80. if(firstChar)
  81. writeParity(firstChar);
  82. else
  83. running = false;
  84. if(running)
  85. cout << firstChar << parityChar << secondChar;
  86. if(!running && firstChar)
  87. cout << firstChar << parityChar;
  88. }
  89. return 0;
  90. }
Add Comment
Please, Sign In to add comment