Advertisement
MtnMCG

oop

Aug 9th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. //ascii > binary
  2. #include <iostream>
  3. #include <string>
  4. #include <bitset>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. char letter = ' ', playAgain = 'y';
  11. string word = " ";
  12.  
  13. cout << "\t**Text To Binary Convertor**\n\n";
  14.  
  15. while (playAgain == 'y'){
  16.  
  17.     cout << "Please enter a character, word, or phrase: ";
  18.  
  19.     getline (cin, word, '\n');
  20.  
  21.     cout << "\nThe binary value for " << word << " is \n";
  22.  
  23.     for (unsigned int wordPosition = 0; wordPosition < word.size(); ++wordPosition){
  24.  
  25.         letter = word[wordPosition];
  26.  
  27.         bitset <8> binary(letter);
  28.  
  29.         cout << binary;
  30.         }
  31.  
  32.  
  33.     cout << "\n\nWould you like to try again? (y/n)";
  34.  
  35.     cin >> playAgain;
  36.  
  37.     if (playAgain != 'y'){
  38.         cout << "\n\nExiting program.";
  39.         playAgain = 'n';
  40.         }
  41.         cin.ignore();
  42. }
  43.  
  44.  
  45.  
  46. return 0;
  47. }
  48. //binary > ascii
  49. #include <iostream>
  50. #include <string>
  51. #include <sstream>
  52. #include <bitset>
  53.  
  54. int main()
  55. {
  56.     std::string data = "01110100011001010111001101110100";
  57.     std::stringstream sstream(data);
  58.     std::string output;
  59.     while(sstream.good())
  60.     {
  61.         std::bitset<8> bits;
  62.         sstream >> bits;
  63.         char c = char(bits.to_ulong());
  64.         output += c;
  65.     }
  66.  
  67.     std::cout << output;
  68.  
  69.    return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement