Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. /**
  2. * @file
  3. * Contains an implementation of the extractMessage function.
  4. */
  5.  
  6. #include <iostream> // might be useful for debugging
  7. #include <assert.h>
  8. #include "extractMessage.h"
  9.  
  10. using namespace std;
  11. char get_bit(const char *input,int start);
  12. char *extractMessage(const char *message_in, int length) {
  13. // Length must be a multiple of 8
  14. assert((length % 8) == 0);
  15.  
  16. // allocates an array for the output
  17. char *message_out = new char[length];
  18. for (int i=0; i<length; i++) {
  19. message_out[i] = 0; // Initialize all elements to zero.
  20. }
  21. //int block = length/8;
  22. for (int i = 0; i < length; i++) {
  23. message_out[i] = message_in[i];
  24. }
  25. //for (int i = 0; i < length; i++) {
  26. //for (int j = 1; j < length/8; j++) {
  27. for (int i = 0; i <= length/8; i++) {
  28. for (int j = i*8; j < i*8+8; j++) {
  29. cout<<"j "<<j<<endl;
  30. message_out[j] = get_bit(message_in, j);
  31. }
  32. //message_out[i*8] = *get_bit(message_in, i*8);
  33. }
  34. //}
  35. // int index = i%8;
  36. // message_out[i] = *get_bit(message_in, index);
  37. // //}
  38. // for (int i = 1; i < block; i++) {
  39. // int index;
  40. // for (int j = 1; j <= 8; j++) {
  41. // //int index;
  42. // index = j*i-1;
  43. // char* shifted = get_bit(message_out, index);
  44. //
  45. // }
  46. // for (int k = 0; k <8; k++) {
  47. //
  48. // }
  49.  
  50.  
  51.  
  52. // TODO: write your code here
  53.  
  54. return message_out;
  55. }
  56. char get_bit (const char *input,int start) {
  57. cout<<"s "<<start<<endl;
  58. int index = start%8;
  59. char mask;
  60. char* copy = new char[8];
  61.  
  62. if (index == 0) {
  63. mask = 0x01;
  64. }
  65. if (index == 1) {
  66. mask = 0x02;
  67. }
  68. if (index == 2) {
  69. mask = 0x04;
  70. }
  71. if (index == 3) {
  72. mask = 0x08;
  73. }
  74. if (index == 4) {
  75. mask = 0x10;
  76. }
  77. if (index == 5) {
  78. mask = 0x20;
  79. }
  80. if (index == 6) {
  81. mask = 0x40;
  82. }
  83. if (index == 7) {
  84. mask = 0x80;
  85. }
  86. for (int i = 0; i < 8; i++) {
  87. copy[i] = input[start- index+i];
  88. cout<<"i: "<<index;
  89. cout<<"index: "<<start-index + i<<endl;
  90. }
  91. for (int i = 0; i < 8; i++) {
  92. cout<<"mask "<<mask<<endl;
  93. copy[i] = copy[i]&mask;
  94. if (i-index < 0) {
  95. cout<<"shift right: "<<index - i<<endl;
  96. copy[i] = copy[i]>>(index - i);
  97. } else {
  98. cout<<"shift left: "<<i - index<<endl;
  99. copy[i] = copy[i]<<(i-index);
  100. }
  101. // cout<<"shift left: "<<i-index<<endl;
  102. // copy[i] = copy[i]<<(i-index);
  103. }
  104. cout<<"input: "<<input<<endl;
  105. cout<<"copy: "<<copy<<endl;
  106. char result;
  107. for (int i = 0; i < 8; i++) {
  108. result = result|copy[i];
  109.  
  110. //cout<<copy[i];
  111. }
  112. cout<<"result: "<<result<<endl;
  113. char m = 0x80;
  114. cout<<"0x20: "<<m<<endl;
  115. return result;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement