By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

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

P: 14
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 :)
Oct 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: 41
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <io.h>
  4.  
  5. char* BYTE2Bin(BYTE val)
  6. {
  7.     char *temp=(char*)calloc(8, sizeof(char));
  8.     for(int i=0; i<8; i++)
  9.     {
  10.         if(((val>>i)&0x01)==1)
  11.             temp[7-i]=0x31;
  12.         else
  13.             temp[7-i]=0x30;
  14.     }
  15.     return temp;
  16. }
  17. void Encode(char *infile, char *outfile)
  18. {
  19.     FILE *file=NULL;
  20.     fopen_s(&file, infile, "rb");
  21.     if(file==NULL)
  22.     {
  23.         cout<<"Error when open file";
  24.         return;
  25.     }
  26.     long filesize=_filelength(file->_file);
  27.     if(filesize<=0)
  28.     {
  29.         cout<<"File length invalid";
  30.         fclose(file);
  31.         return;
  32.     }
  33.     BYTE *buf=(BYTE*)calloc(filesize, sizeof(BYTE));
  34.     if(fread(buf, sizeof(BYTE), filesize, file)<=0)
  35.     {
  36.         cout<<"Error when read file";
  37.         fclose(file);
  38.         free(buf);
  39.         buf=NULL;
  40.         return;
  41.     }
  42.     fclose(file);
  43.     file=NULL;
  44.     // read successfully
  45.     fopen_s(&file, outfile, "wt");
  46.     char *temp=NULL;
  47.     for(int i=0; i<filesize; i++)
  48.     {
  49.         temp=BYTE2Bin(buf[i]);
  50.         fwrite(temp, 8, 1, file);
  51.         free(temp);
  52.         temp=NULL;
  53.     }
  54.     fclose(file);
  55.     file=NULL;
  56.     free(buf);
  57.     buf=NULL;
  58. }
  59.  
  60. void Decode(char *infile, char *outfile)
  61. {
  62.     FILE *file=NULL;
  63.     fopen_s(&file, infile, "rb");
  64.     if(file==NULL)
  65.     {
  66.         cout<<"Error when open file";
  67.         return;
  68.     }
  69.     long filesize=_filelength(file->_file);
  70.     if(filesize<=0)
  71.     {
  72.         cout<<"File length invalid";
  73.         fclose(file);
  74.         return;
  75.     }
  76.     BYTE *buf=(BYTE*)calloc(filesize, sizeof(BYTE));
  77.     if(fread(buf, sizeof(BYTE), filesize, file)<=0)
  78.     {
  79.         cout<<"Error when read file";
  80.         fclose(file);
  81.         free(buf);
  82.         buf=NULL;
  83.         return;
  84.     }
  85.     fclose(file);
  86.     file=NULL;
  87.     // read successfully
  88.     fopen_s(&file, outfile, "wb");
  89.     BYTE temp=0;
  90.     BYTE cnt=0;
  91.     for(int i=0; i<filesize; i++)
  92.     {
  93.         temp|=((buf[i]-0x30)<<(7-cnt));
  94.         cnt++;
  95.         if(cnt==8)
  96.         {
  97.             fwrite(&temp, 1, 1, file);
  98.             cnt=0;
  99.             temp=0;
  100.         }
  101.     }
  102.     fclose(file);
  103.     file=NULL;
  104.     free(buf);
  105.     buf=NULL;
  106. }
  107.  
  108. void main()
  109. {
  110.     Encode("c:\\input.txt", "C:\\encode.txt");
  111.     Decode("C:\\encode.txt", "c:\\decode.jpg");
  112.     _getch();
  113. }
Oct 2 '06 #2

P: 27
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int main () {
  6.   int length;
  7.   char* buffer;
  8.  
  9.   ifstream is;  
  10.   is.open ("vvv.ecc", ios::binary);
  11.  
  12.   is.seekg (0, ios::end);
  13.   length = is.tellg();
  14.   is.seekg (0, ios::beg);
  15.  
  16.   buffer = new char [length];
  17.  
  18.   is.read (buffer, length);
  19.   is.close();
  20.  
  21.   ofstream os;
  22.   os.open ("out.txt", ios::out);  
  23.  
  24.   char* c = buffer; 
  25.   while(length--)
  26.   {    
  27.     for (int i = 0; i < 8; i++)
  28.       os << (((*c<<i)&128) == 128 ? 1 : 0);         
  29.     c++;
  30.     os << " "; 
  31.   }  
  32.  
  33.   delete [] buffer;
  34.   os.close();  
  35.   return 0;
  36. }
  37.  
Try to do backward convertion. Post here if u have problem.
Oct 2 '06 #3

P: 14
thanks !!!
Oct 2 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.