Thread: Outputting as binary, not ASCII

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    162

    Outputting as binary, not ASCII

    I am making a game, and I want to save the stuff, so I use fout, but the problem is that if I have an int that is 150, say, and I use fout, I will output it as ascii text "150", not the binary 10010110. What function do I use to do this? Thanks a lot!

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    So do you want to output on screen or in file in binary ??

    either way you'll have to write some code to convert to binary. there's no functoin that does it auto. for you AFAIK.
    -

  3. #3
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    [stupid_question]what is AFAIK, what does it stand for?[/stupid_question]

    M.R.
    I don't like you very much. Please post a lot less.
    Cheez
    *and then*
    No, I know you were joking. My point still stands.

  4. #4
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    AFAIK == "As Far As I Know"

    ihsir is half right. You will have to write a converter function to output the integer in binary to the screen. However, you can read and write files in binary easily. Here's an example:

    Code:
    #include <fstream>
    
    using std::istream;
    
    int main()
    {
       ifstream infile("stuff.dat", ios::in | ios::binary);
    
       infile.close();
    }
    For further explanation, look at a book or two.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    How would I output 150 as 1 byte(10010110) to a file using ofstream? I think it is ofstream::write(), but I can't figure it out.

  6. #6
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Usually they aren't used to write just a single number or a single character to file. They are usually used to write an array or an object to file. Here is an example of writing an object to file:

    Code:
    #include <iostream>
     using std::cerr;
     using std::endl;
     using std::ios;
    
    #include <fstream>
     using std::ofstream;
    
    // =============================
    // Dummy Class for example
    // =============================
    class MyClass {
       public:
          MyClass(int n, char *s1, char *s2);
    
       private:
          int num;
          char lastName[20];
          char firstName[15];
    };
    
    
    // ==============================
    // Main Function
    // ==============================
    int main()
    {
       ofstream outFile( "stuff.dat", ios::binary );
    
       if( !outFile ) {
          cerr << "File could not be opened." << endl;
          exit( 1 );
       }
    
       MyClass object1(99, "Dade", "Murphy");
    
       outFile.write( reinterpret_cast<const char *>(&object1), sizeof(MyClass) );
    
       outFile.close();
    
       return 0;
    }
    
    
    // ================================
    // Class constructor
    //=================================
    MyClass::MyClass(int n, char *s1, char *s2)
    {
       num = n;
       strcpy(lastName, s2);
       strcpy(firstName, s1);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. help with c program: binary to ascii program
    By bigmac(rexdale) in forum C Programming
    Replies: 26
    Last Post: 02-03-2008, 02:26 PM
  3. Wininet Binary and ASCII
    By maxorator in forum Windows Programming
    Replies: 5
    Last Post: 11-26-2005, 03:16 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM