transforming a binary file in a string of 0s and 1s

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • X~coder
    New Member
    • Sep 2006
    • 14

    transforming a binary file in a string of 0s and 1s

    helo all

    i need urgent help

    i'm trying to tranform a binary file in a sting of 0s and 1s .
    ex: i have the file vvv.ecc how can i tranform it in a strig of 0s and 1s and then put the sting in a txt file?
    and then from that strig to be able to creat the file vvv.ecc exactly as it was?

    pls give the exact code in C++ ( os= xp ) and some specifications ( i' m a nooby but i'm trying to learn :)
  • ltgbau
    New Member
    • Sep 2006
    • 41

    #2
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <io.h>
    
    char* BYTE2Bin(BYTE val)
    {
    	char *temp=(char*)calloc(8, sizeof(char));
    	for(int i=0; i<8; i++)
    	{
    		if(((val>>i)&0x01)==1)
    			temp[7-i]=0x31;
    		else
    			temp[7-i]=0x30;
    	}
    	return temp;
    }
    void Encode(char *infile, char *outfile)
    {
    	FILE *file=NULL;
    	fopen_s(&file, infile, "rb");
    	if(file==NULL)
    	{
    		cout<<"Error when open file";
    		return;
    	}
    	long filesize=_filelength(file->_file);
    	if(filesize<=0)
    	{
    		cout<<"File length invalid";
    		fclose(file);
    		return;
    	}
    	BYTE *buf=(BYTE*)calloc(filesize, sizeof(BYTE));
    	if(fread(buf, sizeof(BYTE), filesize, file)<=0)
    	{
    		cout<<"Error when read file";
    		fclose(file);
    		free(buf);
    		buf=NULL;
    		return;
    	}
    	fclose(file);
    	file=NULL;
    	// read successfully
    	fopen_s(&file, outfile, "wt");
    	char *temp=NULL;
    	for(int i=0; i<filesize; i++)
    	{
    		temp=BYTE2Bin(buf[i]);
    		fwrite(temp, 8, 1, file);
    		free(temp);
    		temp=NULL;
    	}
    	fclose(file);
    	file=NULL;
    	free(buf);
    	buf=NULL;
    }
    
    void Decode(char *infile, char *outfile)
    {
    	FILE *file=NULL;
    	fopen_s(&file, infile, "rb");
    	if(file==NULL)
    	{
    		cout<<"Error when open file";
    		return;
    	}
    	long filesize=_filelength(file->_file);
    	if(filesize<=0)
    	{
    		cout<<"File length invalid";
    		fclose(file);
    		return;
    	}
    	BYTE *buf=(BYTE*)calloc(filesize, sizeof(BYTE));
    	if(fread(buf, sizeof(BYTE), filesize, file)<=0)
    	{
    		cout<<"Error when read file";
    		fclose(file);
    		free(buf);
    		buf=NULL;
    		return;
    	}
    	fclose(file);
    	file=NULL;
    	// read successfully
    	fopen_s(&file, outfile, "wb");
    	BYTE temp=0;
    	BYTE cnt=0;
    	for(int i=0; i<filesize; i++)
    	{
    		temp|=((buf[i]-0x30)<<(7-cnt));
    		cnt++;
    		if(cnt==8)
    		{
    			fwrite(&temp, 1, 1, file);
    			cnt=0;
    			temp=0;
    		}
    	}
    	fclose(file);
    	file=NULL;
    	free(buf);
    	buf=NULL;
    }
    
    void main()
    {
    	Encode("c:\\input.txt", "C:\\encode.txt");
    	Decode("C:\\encode.txt", "c:\\decode.jpg");
    	_getch();
    }

    Comment

    • dush
      New Member
      • Sep 2006
      • 27

      #3
      Hi

      This code will read from your binary file "vvv.ecc" and store binary representation of its bytes in "out.txt"

      for example:

      vvv.ecc :
      abcdefgh

      out.txt:

      01100001 01100010 01100011 01100100 01100101 01100110 01100111 01101000

      Code:
      #include <iostream>
      #include <fstream>
      using namespace std;
      
      int main () {
        int length;
        char* buffer;
      
        ifstream is;  
        is.open ("vvv.ecc", ios::binary);
      
        is.seekg (0, ios::end);
        length = is.tellg();
        is.seekg (0, ios::beg);
      
        buffer = new char [length];
      
        is.read (buffer, length);
        is.close();
      
        ofstream os;
        os.open ("out.txt", ios::out);  
          
        char* c = buffer; 
        while(length--)
        {    
          for (int i = 0; i < 8; i++)
            os << (((*c<<i)&128) == 128 ? 1 : 0);         
          c++;
          os << " "; 
        }  
          
        delete [] buffer;
        os.close();  
        return 0;
      }
      Try to do backward convertion. Post here if u have problem.

      Comment

      • X~coder
        New Member
        • Sep 2006
        • 14

        #4
        thanks !!!

        Comment

        Working...