C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-24-2006, 10:15 AM   #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.
vikernes is offline   Reply With Quote
Old 02-24-2006, 11:35 AM   #2
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
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
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
hk_mp5kpdw is offline   Reply With Quote
Old 02-24-2006, 12:06 PM   #3
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,472
Omg......
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Old 02-24-2006, 01:21 PM   #4
Hardware Engineer
 
Join Date: Sep 2001
Posts: 1,397
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.

Quote:
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.
DougDbug is offline   Reply With Quote
Old 02-24-2006, 04:49 PM   #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.
vikernes is offline   Reply With Quote
Old 02-24-2006, 08:17 PM   #6
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> 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.
Sebastiani is offline   Reply With Quote
Old 02-25-2006, 12:43 AM   #7
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,472
Quote:
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.
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Data Structure Eror prominababy C Programming 3 01-06-2009 09:35 AM
Memory Address kevinawad C++ Programming 18 10-19-2008 10:27 AM
help with text input Alphawaves C Programming 8 04-08-2007 04:54 PM
simulate Grep command in Unix using C laxmi C Programming 6 05-10-2002 04:10 PM
Need a suggestion on a school project.. Screwz Luse C Programming 5 11-27-2001 02:58 AM


All times are GMT -6. The time now is 09:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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