# Thread: Converting Text to Binary (And Back)

1. ## 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.

2. Binary Converter - Various stupid string issues :p

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.

3. 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.

4. > bitset<32> b("a");
bitset<32> b('a');

5. Ahh ok that worked!

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

6. 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!

7. >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;```

8. hmmm alright and one more question. How would I convert binary back to text?

9. 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;
}```

10. 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.

11. 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?

12. But I can't get a string from the b.

13. i dont understand exactly what your wanting, explain a little better?

14. 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.

15. 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.