14 Replies - 8891 Views - Last Post: 13 August 2008 - 09:51 AM Rate Topic: -----

#1 urntme  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-August 08

manipulate raw binary data

Posted 10 August 2008 - 11:40 AM

So suppose I get binary data from a file, byte by byte and I wanted to add this binary data to my own another binary data, how do I go about doing this task? I need to read it un-formatted... any links to sites that describe this would be quite helpful or even suggestions or anything on how to go about it..

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?

Is This A Good Question/Topic? 0
  • +

Replies To: manipulate raw binary data

#2 ChazZeromus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 02-August 08

Re: manipulate raw binary data

Posted 10 August 2008 - 12:55 PM

By bits or bytes? You said all you wanted to do is to save the binary data and now you're saying you want to modify it? :crazy:
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

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: manipulate raw binary data

Posted 10 August 2008 - 03:47 PM

I am not sure what you mean. All computer data is binary so saying "I need to read/write in binary" can be a bit confusing.

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
Was This Post Helpful? 0
  • +
  • -

#4 urntme  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-August 08

Re: manipulate raw binary data

Posted 10 August 2008 - 07:18 PM

Thx for the replies,

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.... :blink:

This post has been edited by urntme: 10 August 2008 - 08:38 PM

Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: manipulate raw binary data

Posted 10 August 2008 - 09:14 PM

Well what format in input.bin in. If you are reading in byte for byte and adding a value thats easy enough. You can read in the file byte for byte , or even read in the entire file into a buffer and manipulate the data there, or you can read chucks of the data and manipulate that.

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;
}

Was This Post Helpful? 0
  • +
  • -

#6 urntme  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-August 08

Re: manipulate raw binary data

Posted 11 August 2008 - 09:48 AM

View PostNickDMax, on 10 Aug, 2008 - 09:14 PM, said:

Well what format in input.bin in. If you are reading in byte for byte and adding a value thats easy enough. You can read in the file byte for byte , or even read in the entire file into a buffer and manipulate the data there, or you can read chucks of the data and manipulate that.

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
!!! :D
Was This Post Helpful? 1

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: manipulate raw binary data

Posted 11 August 2008 - 12:44 PM

:) So would you say that post was Helpful? (hint hint)
Was This Post Helpful? 1
  • +
  • -

#8 urntme  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-August 08

Re: manipulate raw binary data

Posted 12 August 2008 - 09:54 AM

View PostNickDMax, on 11 Aug, 2008 - 12:44 PM, said:

:) So would you say that post was Helpful? (hint hint)


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

Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: manipulate raw binary data

Posted 12 August 2008 - 01:25 PM

The while-loop justs adds 1 to each byte in the buffer. So for example if the test file just contained "ABC" then the buffer would become "BCD".

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).
Was This Post Helpful? 0
  • +
  • -

#10 urntme  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-August 08

Re: manipulate raw binary data

Posted 12 August 2008 - 08:30 PM

View PostNickDMax, on 12 Aug, 2008 - 01:25 PM, said:

The while-loop justs adds 1 to each byte in the buffer. So for example if the test file just contained "ABC" then the buffer would become "BCD".

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... :D :P
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: manipulate raw binary data

Posted 12 August 2008 - 09:01 PM

:) Well yes and No...

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.
Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: manipulate raw binary data

Posted 12 August 2008 - 09:14 PM

by the way your code has two memory- leak errors in it.

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.
Was This Post Helpful? 0
  • +
  • -

#13 urntme  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-August 08

Re: manipulate raw binary data

Posted 13 August 2008 - 07:46 AM

View PostNickDMax, on 12 Aug, 2008 - 09:14 PM, said:

by the way your code has two memory- leak errors in it.

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?
Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: manipulate raw binary data

Posted 13 August 2008 - 09:21 AM

So let me see if I can picture what you want to do. So essentially you want to imagine the contents of this file as one big integer. And you want to preform an operation such as subtraction on this entire number...

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
Was This Post Helpful? 0
  • +
  • -

#15 urntme  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 01-August 08

Re: manipulate raw binary data

Posted 13 August 2008 - 09:51 AM

View PostNickDMax, on 13 Aug, 2008 - 09:21 AM, said:

So let me see if I can picture what you want to do. So essentially you want to imagine the contents of this file as one big integer. And you want to preform an operation such as subtraction on this entire number...

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.. :ph34r:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1