Converting Text to Binary (And Back)

This is a discussion on Converting Text to Binary (And Back) within the C++ Programming forums, part of the General Programming Boards category; I have been searchign and searching and I did find code on this site on how to convert text to ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    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.

    Any help please?

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    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. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    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. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > bitset<32> b("a");
    bitset<32> b('a');

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    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. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    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. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    hmmm alright and one more question. How would I convert binary back to text?

  9. #9
    Banned
    Join Date
    Jun 2005
    Posts
    594
    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. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    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. #11
    Banned
    Join Date
    Jun 2005
    Posts
    594
    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. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    But I can't get a string from the b.

  13. #13
    Banned
    Join Date
    Jun 2005
    Posts
    594
    i dont understand exactly what your wanting, explain a little better?

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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. #15
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    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.
    Last edited by Rune Hunter; 08-24-2005 at 08:58 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  2. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Reading binary files and writing as text
    By thenrkst in forum C++ Programming
    Replies: 8
    Last Post: 03-13-2003, 10:47 PM
  5. Lesson: Binary conversion n back again
    By RoD in forum Windows Programming
    Replies: 16
    Last Post: 09-16-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21