encode mess
I need to encode mess by flipping the bits in each byte.
Woould you help me to solve it, please
This is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstdio>
#include <cctype>
#include <iomanip>
#include <bitset>
using namespace std;
int main()//int argc, char* argv[])
{
string input, line, c;
int count=0, length ;
cout<< " Enter your letter or phrase: ";
getline(cin, line);
length = (int)line.size();
for( count=0; count < length; ++count)
{
bitset<8> foo (line.c_str()[count]);
cout<< (foo.flip()) << '~' ;
}
return 1;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
#include <bitset>
#include <limits>
#include <string>
int main()
{
using bits = std::bitset< std::numeric_limits< unsigned char >::digits > ;
std::string input ;
std::getline( std::cin, input ) ;
std::cout << "input bits:\n" ;
for( unsigned char c : input ) std::cout << bits(c) << ' ' ;
std::cout << "\n\nflipped bits:\n" ;
for( unsigned char c : input ) std::cout << ~bits(c) << ' ' ;
std::cout << '\n' ;
}
|
http://coliru.stacked-crooked.com/a/57386ad67ba23652
thank you so much. However if I do another way by using
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iostream>
using namespace std ;
int main()
{
int c ;
c = cin.get() ;
while ( ! cin.eof() )
{
cout.put(c) ;
c = cin.get() ;
}
}
|
How I do with it ?
Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
#include <bitset>
#include <limits>
int main()
{
using bits = std::bitset< std::numeric_limits< unsigned char >::digits > ;
char c ;
while( std::cin.get(c) && c != '\n' && c != 0 )
{
const unsigned char u = c ;
std::cout << '\'' << c << "' bits: " << bits(u) << " flipped: " << ~bits(u) << '\n' ;
}
}
|
Topic archived. No new replies allowed.