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 :)
3 2930 - #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();
-
}
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 - #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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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$) {
}
...
|
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
|
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...
|
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...
|
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...
| |