473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
3 2930
ltgbau
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
dush
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
X~coder
14
thanks !!!
Oct 2 '06 #4

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

Similar topics

1
by: Xamalaek | last post by:
Hello, Is it possible to transform an XML document to a binary format using XSL? Can one use XSL-FO to do this? Regards, X
4
by: Cathie | last post by:
Hi All, I am trying to get my style sheet to work. It works fine in IE but I can't get it to work in .net. Below is the function I use for transforming, where advancedOptionsFile is the path...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.