manipulate raw binary data
Page 1 of 114 Replies - 8891 Views - Last Post: 13 August 2008 - 09:51 AM
#1
manipulate raw binary data
Posted 10 August 2008 - 11:40 AM
I already know how to read the binary data in binary mode, but I want to output the binary bits I read in binary itself and then even modify this data and then save it as another file, my problem is with the binary manipulation part of the thing..
So... any suggestions?
Replies To: manipulate raw binary data
#2
Re: manipulate raw binary data
Posted 10 August 2008 - 12:55 PM
What are you using to read binaries?
So you want read byte by byte and then save it to another file having each byte modified? Well this is easy if you use the standard File I/O C libraries. I'll try to write and example that well read byte by byte, but for speed you should read from the file into a large buffer and then manipulate the buffer without going to read each byte. So here's an example:
#include<stdio.h>
#define BUFFER 1024
//Encrypts a file by flipping all bytes, i.e XOR
int main(int argc, char** argv)
{
char* input, *output;
FILE* fin, *fout;
void* buffer;
size_t read;
if (argc != 3)
return 1;
input = argv[1];
output = argv[2];
fopen_s(&fin, input, "rb");
if (!fin)
return 1;
fopen_s(&fout, output, "wb");
if (!fout)
return 1;
buffer = new char[BUFFER];
while (!feof(fin)) {
read = fread(buffer, 1, BUFFER, fin);
if (!read)
break;
for (size_t i = 0; i < read; ++i) {
buffer[i] = ~buffer[i];
}
fwrite(buffer, 1, read, fout);
}
fclose(fin);
fclose(fout);
delete [] buffer;
return 0;
}
I didn't compile, I just wrote it ontop of my head.
This post has been edited by ChazZeromus: 10 August 2008 - 01:14 PM
#3
Re: manipulate raw binary data
Posted 10 August 2008 - 03:47 PM
When reading/writing to files there are two general modes, ASCII (text) and Binary (pure data not interpreted as text). While the former is quite popular the latter is generally more useful.
So I assume that you are talking about opening a file for Binary reading and writing. So you need to to use fread() to read the content of a file directly into a variable.
The next part of your question seem to ask how to manipulate data at a bit level. There are a number of ways to do this but in the end they all boil down to binary arithmetic. For example lets suppose that I had a byte where the high nibble was one value and the low nibble was another value:
char A = 192; char low = A & 0xF; // get the value of the low nibble by masking out the top nibble. char high = (A & 0xF0 ) >> 4; //get the value of the high nibble (masking out the low nibble).
C/C++ also has something called a bit-field which can be used to access data at a bit level.
C/C++ also has the concept of a enumeration which is often used to access bit-field type data.
If you help us understand exactly what you are trying to do we can help a little more
#4
Re: manipulate raw binary data
Posted 10 August 2008 - 07:18 PM
will check it out..
But that is what I want..
there's a file input.bin
I read the un-formatted as in non-ASCII, raw binary value of the file.
Then I add that binary value to my own binary number, that I have previously stored.
Then I output this and save it as some output.bin
As in:
input.bin
10101012210 <- read data
+ 00110111 <- added data
save sum as output.bin
I use this to read the file
src.read(memblock, num_of_bytes);
and write to write it, is this wrong?
---- edited
okay.. thx for the links and the sample code, checked it out... but it seems like fread() and fopen() are not what I am looking for, since they are also used to read formatted data..
so now... I use this...
// istream get
#include <iostream>
#include <fstream>
#include<conio.h>
using namespace std;
int get();
int main () {
int c;
ifstream is;
is.open ("e:\\ananths documents\\code\\input.bin"); // open file
while (is.good()) // loop while extraction from file is possible
{
c = is.get(); // get character from file
cout << c;
}
is.close(); // close file
getche();
return 0;
}
But there's another problem with this, when I output it, I get it only in numbers, as in 1 to 9, normal decimal numbers.
But I wanted it in Binary, to see the raw binary data... What am I doing wrong?
--- edited
But according the fread link, I get raw unformatted data into the buffer, so how do I go about outputting that?
Im sooooo confused right now....
This post has been edited by urntme: 10 August 2008 - 08:38 PM
#5
Re: manipulate raw binary data
Posted 10 August 2008 - 09:14 PM
You do have some question to cover (like what do you do in case of an overflow).
here is a basic C++ example. The process is very much the same:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int length;
char *buffer;
char *ptr;
fstream impFile;
impFile.open("input.bin", fstream::binary | fstream::in | fstream::out );
if (impFile) {
cout << "File Opened..." << endl;
// get length of file:
impFile.seekg (0, ios::end);
length = impFile.tellg();
impFile.seekg (0, ios::beg);
cout << "File Length: " << length << endl;
//Allocate a buffer large enough to hold the file...
buffer = new char[length + 1];
impFile.read(buffer, length);
ptr = buffer + length;
cout << "File contains: ";
cout.write(buffer, length);
cout << endl;
//Add 1 to each byte in the file...
while ( ptr-- >= buffer) { (*ptr)++; }
cout << "Writing to the file: ";
cout.write(buffer, length);
cout << endl;
impFile.seekg (0, ios::beg);
impFile.write(buffer, length);
//close the file...
impFile.close();
//release the buffer...
delete[] buffer;
} else {
cout << "File did not open" << endl;
}
return 0;
}
#6
Re: manipulate raw binary data
Posted 11 August 2008 - 09:48 AM
NickDMax, on 10 Aug, 2008 - 09:14 PM, said:
You do have some question to cover (like what do you do in case of an overflow).
here is a basic C++ example. The process is very much the same:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int length;
char *buffer;
char *ptr;
fstream impFile;
impFile.open("input.bin", fstream::binary | fstream::in | fstream::out );
if (impFile) {
cout << "File Opened..." << endl;
// get length of file:
impFile.seekg (0, ios::end);
length = impFile.tellg();
impFile.seekg (0, ios::beg);
cout << "File Length: " << length << endl;
//Allocate a buffer large enough to hold the file...
buffer = new char[length + 1];
impFile.read(buffer, length);
ptr = buffer + length;
cout << "File contains: ";
cout.write(buffer, length);
cout << endl;
//Add 1 to each byte in the file...
while ( ptr-- >= buffer) { (*ptr)++; }
cout << "Writing to the file: ";
cout.write(buffer, length);
cout << endl;
impFile.seekg (0, ios::beg);
impFile.write(buffer, length);
//close the file...
impFile.close();
//release the buffer...
delete[] buffer;
} else {
cout << "File did not open" << endl;
}
return 0;
}
Wow! thx for the code... that really clears things up... I want to do something simillar only... cool... You have my sincearest thanks
!!!
#7
Re: manipulate raw binary data
Posted 11 August 2008 - 12:44 PM
#8
Re: manipulate raw binary data
Posted 12 August 2008 - 09:54 AM
NickDMax, on 11 Aug, 2008 - 12:44 PM, said:
just one thing...
could you explain how this part of the code works?
# while ( ptr-- >= buffer) { (*ptr)++; }
# cout << "Writing to the file: ";
# cout.write(buffer, length);
# cout << endl;
# impFile.seekg (0, ios::beg);
# impFile.write(buffer, length);
p.s
hint taken... [
This post has been edited by urntme: 12 August 2008 - 09:54 AM
#9
Re: manipulate raw binary data
Posted 12 August 2008 - 01:25 PM
The cout.write(buffer, length) displays the buffer (sends the contents of the buffer to the stdout).
The seekg() file resets the file pointer to the beginning of the file.
The next line simply writes the data in the buffer back out to the file. (starting at the begining of the the file since the seekg() reset our pointer to the beginning of the file).
#10
Re: manipulate raw binary data
Posted 12 August 2008 - 08:30 PM
NickDMax, on 12 Aug, 2008 - 01:25 PM, said:
The cout.write(buffer, length) displays the buffer (sends the contents of the buffer to the stdout).
The seekg() file resets the file pointer to the beginning of the file.
The next line simply writes the data in the buffer back out to the file. (starting at the begining of the the file since the seekg() reset our pointer to the beginning of the file).
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
char *memblock;
double num_of_bytes = 1; //any user specific number of bytes
memblock = new char [num_of_bytes];
//source file
ifstream src("E:\\Ananths Documents\\Code\\input.bin",ios::in | ios::binary);
//dest file
ofstream dest("E:\\Ananths Documents\\Code\\Binary_File_IO\\Binary File IO\\output.bin",ios::app | ios::binary);
//if we couldn't open the source file (it may not exist), means fail
if(!src.is_open())
{
cout<<"Failed to open the file.\n";
return 0;
}
cout << "The file successfully opened.\n";
while(true)
{
//reading "num_of_bytes" byte(s) from sourcefile
src.read(memblock, num_of_bytes);
cin>>src(*this, num_of_bytes);
//counting number of bytes actually read
//using gcount() function
num_of_bytes = src.gcount();
//if zero bytes read, means End Of File
//has reached, no bytes left to read
if(num_of_bytes == 0) break;
//writing "num_of_bytes" byte(s) to destination file
dest.write(memblock,num_of_bytes);
}
//closing source and destination files.
src.close();
dest.close();
cout << "\nThe file was successfully copied.\n";
delete memblock;
cout<<"pause";
return 0;
}
I tried using this to copy the contents of the file. But I can only do it in chunks, Is there any way to do it bit by bit? not byte, bit...
Thanks for all the help so far by the way.. From now I shall call you my sensei...
#11
Re: manipulate raw binary data
Posted 12 August 2008 - 09:01 PM
Computers don't really work well bit-by-bit. More or less the smallest sort of "package" is a byte. To that does not mean that you can't access each bit of a byte.
For example:
char data = 0xf5; // some byte to manipulate
int i = 8; //# of bits in a byte
while (i--) {
cout << (data & 0x01) << " ";
data >>= 1; //shift the bits down 1 space...
}
//Note the display is low bits (left) to high bits (right).
cout << endl;
short int sidata = 0xf5AC;
i = sizeof(sidata)*8; //number of bits in a short int...
while (i--) {
cout << (sidata & 0x01) << " ";
sidata >>= 1; //shift the bits down 1 space...
}
cout << endl;
So if you wanted to read a file in bit for bit then you would read in a byte, and then use a technique like this to get each bit.
of course there are other ways to access the various bits of a byte or word or long etc.
#12
Re: manipulate raw binary data
Posted 12 August 2008 - 09:14 PM
First of all, you are using new [] to allocate an array, but not using delete[] to delete the array.
Next when you call return when the source file did not open, you never delete[] the memory you allocated. You must ensure that ALL exit paths free the allocated memory.
#13
Re: manipulate raw binary data
Posted 13 August 2008 - 07:46 AM
NickDMax, on 12 Aug, 2008 - 09:14 PM, said:
First of all, you are using new [] to allocate an array, but not using delete[] to delete the array.
Next when you call return when the source file did not open, you never delete[] the memory you allocated. You must ensure that ALL exit paths free the allocated memory.
okay will do...
But could you elaborate some more on the bit thing?
I need to do a binary subtraction on the whole file. As in I need to view the whole file in binary mode, use it as a binary number and subtract another huge binary number from it... So, how do I go about it?
If its a bit by bit thing, then I can do it since I have an algorithm for that in mind, is there way to do it?
#14
Re: manipulate raw binary data
Posted 13 August 2008 - 09:21 AM
This sounds like you are trying to do arbitrary precision arithmetic. Although you could presue your current path you may find learning about arbitrary precision arithmetic may help.
There is hardly a need to do this bit-by-bit
#15
Re: manipulate raw binary data
Posted 13 August 2008 - 09:51 AM
NickDMax, on 13 Aug, 2008 - 09:21 AM, said:
This sounds like you are trying to do arbitrary precision arithmetic. Although you could presue your current path you may find learning about arbitrary precision arithmetic may help.
There is hardly a need to do this bit-by-bit
hmmm.... will look into it... arbitrary precision mathematics huh... sounds very arbitrary..
|
|

New Topic/Question
Reply




MultiQuote





|