fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. int main(void)
  9. {
  10. char* input = "01001001001000000110110001101001011010110110010100100000011110010110111101110101";
  11. int length = strlen(input); //get length of string
  12.  
  13. int binary[8]; //array used to store 1 byte of binary number (1 character)
  14. int asciiNum = 0; //the ascii number after conversion from binary
  15. char ascii; //the ascii character itself
  16.  
  17. cout << " ";
  18.  
  19. int z = 0; //counter used
  20.  
  21. for(int x = 0; x < length / 8; x++) //reading in bytes. total characters = length / 8
  22. {
  23. for(int a = 0; a < 8; a++) //store info into binary[0] through binary[7]
  24. {
  25. binary[a] = (int) input[z] - 48; //z never resets
  26. z++;
  27. }
  28.  
  29. int power[8]; //will set powers of 2 in an array
  30. int counter = 7; //power starts at 2^0, ends at 2^7
  31. for(int x = 0; x < 8; x++)
  32. {
  33. power[x] = counter; //power[] = {7, 6, 5, ..... 1, 0}
  34. counter--; //decrement counter each time
  35. }
  36.  
  37. for(int y = 0; y < 8; y++) //will compute asciiNum
  38. {
  39. double a = binary[y]; //store the element from binary[] as "a"
  40. double b = power[y]; //store the lement from power[] as "b"
  41.  
  42. asciiNum += a* pow(2, b); //asciiNum = sum of a * 2^power where 0 <= power <= 7, power is int
  43. }
  44.  
  45. ascii = asciiNum; //assign the asciiNum value to ascii, to change it into an actual character
  46. asciiNum = 0; //reset asciiNum for next loop
  47.  
  48. cout << ascii; //display the ascii character
  49. }
  50.  
  51.  
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
 I like you