Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @file
- * Contains an implementation of the extractMessage function.
- */
- #include <iostream> // might be useful for debugging
- #include <assert.h>
- #include "extractMessage.h"
- using namespace std;
- char get_bit(const char *input,int start);
- char *extractMessage(const char *message_in, int length) {
- // Length must be a multiple of 8
- assert((length % 8) == 0);
- // allocates an array for the output
- char *message_out = new char[length];
- for (int i=0; i<length; i++) {
- message_out[i] = 0; // Initialize all elements to zero.
- }
- //int block = length/8;
- for (int i = 0; i < length; i++) {
- message_out[i] = message_in[i];
- }
- //for (int i = 0; i < length; i++) {
- //for (int j = 1; j < length/8; j++) {
- for (int i = 0; i <= length/8; i++) {
- for (int j = i*8; j < i*8+8; j++) {
- cout<<"j "<<j<<endl;
- message_out[j] = get_bit(message_in, j);
- }
- //message_out[i*8] = *get_bit(message_in, i*8);
- }
- //}
- // int index = i%8;
- // message_out[i] = *get_bit(message_in, index);
- // //}
- // for (int i = 1; i < block; i++) {
- // int index;
- // for (int j = 1; j <= 8; j++) {
- // //int index;
- // index = j*i-1;
- // char* shifted = get_bit(message_out, index);
- //
- // }
- // for (int k = 0; k <8; k++) {
- //
- // }
- // TODO: write your code here
- return message_out;
- }
- char get_bit (const char *input,int start) {
- cout<<"s "<<start<<endl;
- int index = start%8;
- char mask;
- char* copy = new char[8];
- if (index == 0) {
- mask = 0x01;
- }
- if (index == 1) {
- mask = 0x02;
- }
- if (index == 2) {
- mask = 0x04;
- }
- if (index == 3) {
- mask = 0x08;
- }
- if (index == 4) {
- mask = 0x10;
- }
- if (index == 5) {
- mask = 0x20;
- }
- if (index == 6) {
- mask = 0x40;
- }
- if (index == 7) {
- mask = 0x80;
- }
- for (int i = 0; i < 8; i++) {
- copy[i] = input[start- index+i];
- cout<<"i: "<<index;
- cout<<"index: "<<start-index + i<<endl;
- }
- for (int i = 0; i < 8; i++) {
- cout<<"mask "<<mask<<endl;
- copy[i] = copy[i]&mask;
- if (i-index < 0) {
- cout<<"shift right: "<<index - i<<endl;
- copy[i] = copy[i]>>(index - i);
- } else {
- cout<<"shift left: "<<i - index<<endl;
- copy[i] = copy[i]<<(i-index);
- }
- // cout<<"shift left: "<<i-index<<endl;
- // copy[i] = copy[i]<<(i-index);
- }
- cout<<"input: "<<input<<endl;
- cout<<"copy: "<<copy<<endl;
- char result;
- for (int i = 0; i < 8; i++) {
- result = result|copy[i];
- //cout<<copy[i];
- }
- cout<<"result: "<<result<<endl;
- char m = 0x80;
- cout<<"0x20: "<<m<<endl;
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement