convert word into Binary code
Nov 13, 2015 at 6:50am UTC
I need convert word into Binary code, however after converting, O change to 1 and 1 change to 0. I used bitset is main code to change. However it doesn't work. SOmeone help me please
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 <string>
#include <bitset>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[], char **env)
{
string c;
c =cin.get();
while (!cin.eof())
{
for ( int i = 0; i < c.size() ; ++i)
{
bitset <8> foo (c.c_str()[i]);
cout.put(foo.flip()) ;
}
};
return 0;
}
Last edited on Nov 13, 2015 at 10:24pm UTC
Nov 13, 2015 at 12:48pm UTC
What exactly does not work?
Last edited on Nov 13, 2015 at 12:49pm UTC
Nov 13, 2015 at 3:32pm UTC
Are you just trying to display the bits to the console? If so, replace this
with this
cout.put must take a char as a parameter (You're entering in a bitset).
Also, I don't think you want to do
I think that guarantees an infinite loop.
Nov 13, 2015 at 10:24pm UTC
i got foo.flip() problem. However, if dont use while(!cin.eof) this look stop at first letter. May you suggest something ? And I look up in while(!!cin.eof) is doesn't stop running repeat look the first letter. How to stop it and look the next word?
Nov 13, 2015 at 11:55pm UTC
1 2 3 4 5
char c;
while (std::cin>>c){
std::bitset<8> foo(c);
//...
}
Nov 14, 2015 at 2:08am UTC
So you are trying to look at entire words and there can be more than one word.
What would you want to see if I inputted the following,
Would you rather see something like this?
01000011 01100001 01110100 // Cat
01001000 01100001 01110100 // Hat
01000010 01100001 01110100 // Bat
Or this?
01000011 01100001 01110100 00100000 01001000 01100001 01110100 00100000 01000010 01100001 01110100 // Cat Hat Bat (Spaces included in binary)
Regardless, I think one possible solution is simply that you use the class sstream. This allows you to read through an entire stream.
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 29 30
#include <string>
#include <bitset>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <sstream>
using namespace std;
int main(int argc, char *argv[], char **env)
{
string c;
getline(cin, c);
stringstream StreamC(c); // Pass c as the string to stream
char bitChar;
while (StreamC >> bitChar)
{
bitset <8> foo(bitChar);
cout << foo << " " ;
};
return 0;
}
Now if I input
I will get
01000011 01100001 01110100 00100000 01001000 01100001 01110100 00100000 01000010 01100001 01110100 // Cat Hat Bat (Spaces included in binary)
Last edited on Nov 14, 2015 at 2:08am UTC
Nov 23, 2015 at 4:43pm UTC
In the same requirement. If I use a waterpump program skeleton:
1 2 3 4 5 6 7 8 9 10 11
int main(int argc, char *argv[])
{
int c = cin.get() ;
while (!cin.eof())
{
(logic of a conditional nature)
cout.put(c) ;
c = cin.get() ;
}
}
I finish my code, however, It ins't same with waterpump
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
#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;
}
Can you help me?
Last edited on Nov 23, 2015 at 4:43pm UTC
Topic archived. No new replies allowed.