Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ascii > binary
- #include <iostream>
- #include <string>
- #include <bitset>
- using namespace std;
- int main()
- {
- char letter = ' ', playAgain = 'y';
- string word = " ";
- cout << "\t**Text To Binary Convertor**\n\n";
- while (playAgain == 'y'){
- cout << "Please enter a character, word, or phrase: ";
- getline (cin, word, '\n');
- cout << "\nThe binary value for " << word << " is \n";
- for (unsigned int wordPosition = 0; wordPosition < word.size(); ++wordPosition){
- letter = word[wordPosition];
- bitset <8> binary(letter);
- cout << binary;
- }
- cout << "\n\nWould you like to try again? (y/n)";
- cin >> playAgain;
- if (playAgain != 'y'){
- cout << "\n\nExiting program.";
- playAgain = 'n';
- }
- cin.ignore();
- }
- return 0;
- }
- //binary > ascii
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <bitset>
- int main()
- {
- std::string data = "01110100011001010111001101110100";
- std::stringstream sstream(data);
- std::string output;
- while(sstream.good())
- {
- std::bitset<8> bits;
- sstream >> bits;
- char c = char(bits.to_ulong());
- output += c;
- }
- std::cout << output;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement