Converting Text to Binary (And Back)

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-24-2005
Rune Hunter
Converting Text to Binary (And Back)
I have been searchign and searching and I did find code on this site on how to convert text to binary but that code didn't seem to work... at all. It also didn't seem like it could convert back.

I just need a website or somthing that teaches how to do this, I can only find converters that do this for you.

• 08-24-2005
ILoveVectors

here a post that was done a couple days ago,
you can look at my post, the show easy way using bitset,
to show the binary value.

bitset also let you take a string of 0's and 1's to create
a num/letter/string . the bitset i used was <32> becuase
he was using numbers, but you can change it to 8 or any other
number of places.
• 08-24-2005
Rune Hunter
Alright well now I think I am getting somewere but here is the code I made from yours...

Code:

```#include <iostream> #include <bitset> #include <string> using namespace std; int main() {     bitset<32> b(3);         cout << b << endl;         cin.get();         return 0; }```
it is watered down just so I can easily test it. But this is the line of code I am confused about:

bitset<8> b(3);

How do I use that? if I put b("a") it just gives an error.
• 08-24-2005
swoopy
> bitset<32> b("a");
bitset<32> b('a');
:confused:
• 08-24-2005
Rune Hunter
Ahh ok that worked!

I'm guessing it can't do a whole string and that is why you do it character by character?
• 08-24-2005
Rune Hunter
Ahh ok that worked!

I'm guessing it can't do a whole string and that is why you do it character by character?

Anyways thanks alot!
• 08-24-2005
swoopy
>I'm guessing it can't do a whole string and that is why you do it character by character?

I don't think you can do a whole string. You'd have to use a vector of bitset, then use a loop to print each element (or use the copy algorithm to copy to cout).
Code:

```  vector < bitset<8> > b;       b.push_back('D');   b.push_back('O');   b.push_back('G');           for (int i=0; i<b.size(); i++)       cout << b[i] << endl;```
• 08-24-2005
Rune Hunter
hmmm alright and one more question. How would I convert binary back to text?
• 08-24-2005
ILoveVectors
Code:

```#include <bitset> #include <iostream> #include <string> using namespace std; int main() {   bitset<32> b ( string ( "11111111" ) );   cout<< b <<'\n'     << static_cast<char> ( b.to_ulong() ) <<'\n'     << static_cast<int> ( b.to_ulong() ) <<'\n';   cin.get();   return 0; }```
• 08-24-2005
Rune Hunter
alright I got one last problem. How do I turn b (the binary number) into a LPSTR or string. I can't type cast it so I'm kinda lost on that. And turning into a number won't work for my purposes.
• 08-24-2005
ILoveVectors
if you have a bunch of letter that you turned to binary, just
go thru with a loop and add teh to a string then you can
have a string?
• 08-24-2005
Rune Hunter
But I can't get a string from the b.
• 08-24-2005
ILoveVectors
i dont understand exactly what your wanting, explain a little better?
• 08-24-2005
swoopy
I'm not sure if this helps or not, but you can go from string to b, so I'm not sure you would need to go the other way, since you already have a string.
Code:

```#include <iostream> #include <string> #include <vector> #include <bitset> using namespace std; int main() {   string food("PIZZA");   vector < bitset<8> > b;   for (int i=0; i<food.length(); i++)       b.push_back(food[i]);           for (int i=0; i<b.size(); i++)       cout << b[i] << endl; }```
It's possible to go from b to string, but not trivial. At least I can't think of a quick way. But if you need to, it can be done.
• 08-24-2005
Rune Hunter
I want to do this (This is for a dll)

return (LPSTR) b;

Sence that doesn't work I need anouther way if bossible.

EDIT: Ok I will explain this again but better.

b contains the binary 01100110. I need to make b a string containing that binary so I can return it as a string.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last