![]() |
| | #1 |
| Registered User Join Date: Dec 2005
Posts: 18
| Print file in binary mode 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;
}
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 | |
| | #2 |
| Registered User 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;
}
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 | |
| | #3 |
| Super Moderator 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 | |
| | #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:
| |
| DougDbug is offline | |
| | #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;
}
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 | |
| | #6 |
| Guest 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 will report the size of a pointer on your system, not the size of the memory the pointer points to. |
| Sebastiani is offline | |
| | #7 | |
| Super Moderator Join Date: Aug 2001
Posts: 7,472
| Quote:
__________________ If you aim at everything you will hit something but you won't know what it is. | |
| Bubba is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |