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; You can use the bitset member to_ulong to make the conversion. Code: #include <iostream> #include <string> #include <vector> #include <bitset> ...

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You can use the bitset member to_ulong to make the conversion.
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <bitset>
    
    using namespace std;
    
    int main()
    {
       string pizza("CHEESE");
       vector < bitset<8> > b;
    
       for (int i=0; i<pizza.length(); i++)
          b.push_back(pizza[i]);
            
       for (int i=0; i<b.size(); i++)
          cout << b[i] << endl;
    
       string pizza_name;
       for (int i=0; i<b.size(); i++)
       {
          char c = b[i].to_ulong();
          //cout << (int) c << endl;
          pizza_name = pizza_name + c;
       }
       cout << pizza_name << endl;
    
    }

  2. #17
    Registered User
    Join Date
    Aug 2005
    Posts
    91
    My knowledge of C++ only goes back a single day (that's how long I've been doing C++, ouch), so don't blame me if I'm wrong, but can you typecast or use a pointer?

  3. #18
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Well I said I can not type cast in about 3 or so other posts, gives me an error. And now that I have a string...... Game Maker gives me a stupid error. One problem after the other.

    Also if I just return it as (float) b.to_ulong it gives me the acsii code for the letter. Why is it giving me the ascii code and not binary?

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And I think an LPSTR is basically a char array, once you have a string, then you can store that into a char array:
    Code:
    char carray[100];
    strcpy( carray, pizza_name.c_str() );
    Or if your LPSTR already has memory allocated for it, then use strcpy() to copy to the LPSTR.

    EDIT: added c_str()

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >(float) b.to_ulong
    You don't want float, you want char:
    (char) b.to_ulong()
    That will give you one char, which you could store straight to a char array, if you wish.

  6. #21
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Allright I think all problems on the C++ side are fixed so the rest is up to me now.

    Thanks alot!

  7. #22
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    #include <bitset>
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    
    int main()
    {
    	string binary;
    	stringstream ss;
    	bitset<8> b('a');
    	ss << b;
    	binary = ss.str();
    	cout << binary << endl;
    	cin.get();
    	return 0;
    }
    is this the kind of string you wanted, the string of 1's and 0's?
    Last edited by ILoveVectors; 08-24-2005 at 11:41 PM.

Page 2 of 2 FirstFirst 12
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