Print file in binary mode

This is a discussion on Print file in binary mode within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a program that would read any file (in binary mode) and print, or write as a ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    18

    Print file in binary mode

    I'm trying to write a program that would read any file (in binary mode) and print, or write as a text file, the bits of the program.

    Is there a function in STD that would do something like that?

    My program doesn't do much at this point. It just reads a file in binary mode and stores the data as a char*.

    I guess what I need right now is the fastest way to convert that char pointer into binary digits. The digits will probably be in a char array - by groups of 8.

    Code:
    ifstream::pos_type size;
    char* mem;
    
    int main ()
    {
    	ifstream file("test.dll", ios::in|ios::binary|ios::ate);
    	size = file.tellg();
    	mem = new char[size];
    
    	file.seekg (0, ios::beg);
    	file.read (mem, size);
    	file.close();
    
            delete[] mem;
    	return 0;
    }
    So how would I reliably and quickly convert all those chars to binary digits.

    The plan is to open any file, store its 'binary digits' in a text file and be able to later open that text file and convert those binary digits into the same file.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Using a bitset is an easy way (maybe not the fastest) to convert the characters into binary strings of 0's and 1's:

    Code:
    #include <bitset>
    
    ...
    
    char  word[] = "Hello";
    char* cptr = word;
    while( *cptr )
    {
        cout << *cptr << " = " << bitset<8>(*cptr) << endl;
        ++cptr;
    }
    Output:
    Code:
    H = 01001000
    e = 01100101
    l = 01101100
    l = 01101100
    o = 01101111
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Omg......

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Maybe you just need a hex editor. In any case, a hex editor should help you with your project.

    A hex editor will show you the contents of a file in hex, and it will also display the ASCII character for any values that convert to ASCII. I have a free one called XVI32.

    Hex is usually preferred over binary (base-2).
    - Binary gets difficult to read when you are dealing with numbers larger than 8-bits.
    - Binary is more difficult to type.
    - Hex is built into cin and cout. Binary is not.
    - You can easily learn to convert between hex and binary (any size number) in your head. You need a calculator to convert between decimal and binary.

    What is a hex editor?

    A hex editor is a program which allows you to edit compiled programs and binary data-files.

    These editors are called hex editors because they most often present data in hexadecimal format. Hexadecimal is used because it is easier for most humans than working in binary. In addition, hexadecimal is frequently useful because computers tend to work with 8-bit bytes of information and because ASCII is an 8-bit code.

    Some hex editors are also able to edit raw disk partitions and other file system structures.

    Hex editors can sometimes be used to remove copy protection. They can also sometimes be used to cheat at computer games by editing saved games and character files.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    Thanks a bunch hk_mp5kpdw, that settled it. But I'm getting some strange behavior in my code. Below is my entire code for this procedure:

    Code:
    #include <iostream>
    #include <fstream>
    #include <bitset>
    using namespace std;
    
    ifstream::pos_type size;
    char* mem;
    
    int main ()
    {
    	ifstream file("test.dll", ios::in|ios::binary|ios::ate);
    	size = file.tellg();
    	mem = new char[size];
    
    	file.seekg(0, ios::beg);
    	file.read(mem, size);
    
    	char* cptr = mem;
    	while(*cptr)
    	{
    		cout << *cptr << " = " << bitset<8>(*cptr) << endl;
    		++cptr;
    	}
    
    	cout << "Size is: " << size << " bytes";
    	cout << endl;
    	file.close();
        delete[] mem;
    	return 0;
    }
    The problem is that when I try file.open with test.txt (as simple text file) the program seems to work OK, exen with large txt files (eceedeing the size of the test.exe or test.dll). However when testing with either test.dll or test.exe file the program outputs only first three values. I've looked at the file with a hex editor and the program is indeed correct about the first 3 values, but why is it only outputting the first 3?

    I've already tried putting a cin.good() in there and it says it's OK. A sizeof(mem) outputs as 4, which is obviously wrong considering the test.exe is 60 KB.

    It's been quite some time since I've done some programming so I don't know what that limit is or how to overcome it
    Should I store the file in memory as a different type?


    I am really tired now and just wanted to post this, I will post an update and try toying with it further tomorrow.

    DougDbug Thanks for helping, but this is some sort of a personal experiment, in the future I'll probably store them as hex values.
    And yes, I'm already usin XVI32, I think it's great.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    >> while(*cptr)

    the loop terminates when it encounters the value zero (incidentally, the fourth byte of an exe/dll is zero). just set another pointer to the end of data and loop on that condition.

    Code:
    char* cptr = mem, * cend = cptr + size;
    while(cptr != cend)
    >> sizeof(mem)

    sizeof will report the size of a pointer on your system, not the size of the memory the pointer points to.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    The plan is to open any file, store its 'binary digits' in a text file and be able to later open that text file and convert those binary digits into the same file.
    I guess you guys didn't read this line. A clear indication of a misunderstanding of hex, binary, and files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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