473,386 Members | 2,129 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,386 software developers and data experts.

Coversion of Binary file to ASCII file using C.

Hi,

I am able to open a binary file for reading but can someone tell me as
how to go about converting a Binary file to ASCII file using C.

Thanks In Advance
SandyMan

May 3 '06 #1
6 10005
"SandyMan" <rs*********@gmail.com> wrote:
I am able to open a binary file for reading but can someone tell me as
how to go about converting a Binary file to ASCII file using C.


Not until you decide what you want the conversion to actually _do_. A
binary file (as opposed to a binary stream, which is the other side of
the medal, as it were) is generally supposed to consist of any possible
data, including some that has no equivalent printable text character.
One can always, of course, "convert" a binary file to text by means of a
hex dump or something similar; but is that really what you want to do,
or was there some other kind of conversion you were thinking of?

Richard
May 3 '06 #2
The input to my C program is a Binary file consisting of certain
variables and its corresponding value.All i want to do is to read the
the variable name and its associated value one by one in ASCII.

SandyMan

May 3 '06 #3
I have to do the following in C

I need to

A)Open a file (a binary file)
B) Read a field ( i know the offset and length)
C) Convert the value to Ascii /Hex
D)And then change the value of the field (which i have in Hex)
E) Convert it to Ascii
F)And put in the original file.

Till now I just know how to open a file
FILE *fopen(const char *filename, const char *mode);
like i have coeded as f=fopen("/user/binaryfile","rb")

to read i have used
ret = fread(ptr,1,2,f);
where ptr is
byte ptr[2];

But i am not sure of how to use binary arrays how to get value from
these and also how to write data in these
which will then be used to write data in the file

I have to write a filed of length 13 eg 0604040404011

I also know the offset of the fleld

I am exploring fseek with SEEK_SET for the beginning of the file
main problem currently is handling of binary arrays (Byte array)

Here is my complete code

#include<stdio.h>

void main(){
typedef unsigned char byte;
/* Define the "byte" type name to be an unsigned char. */
byte ptr[2];
/* Declare a two byte array. */

int ret;

FILE* f; // create a new file pointer
if((f=fopen("/user/binaryfile","rb"))==NULL) { //
printf("could not open file"); // print
exit(1);
}

ret = fread(ptr,1,2,f);
printf("%d",ret);
printf("%o",*ptr);
fclose(f); // close the filepointer
}
Please tell me how to convert the binary data to Ascii/Hex data and
also back to binary

Thanks

May 3 '06 #4
"SandyMan" writes:
I have to do the following in C

I need to

A)Open a file (a binary file)
B) Read a field ( i know the offset and length)
C) Convert the value to Ascii /Hex
D)And then change the value of the field (which i have in Hex)
E) Convert it to Ascii
F)And put in the original file.

<snip>

What if someone just told you to do this, and that person (and you) didn't
even know that binary files existed? Would you have any question in that
case?

Binary files have a terribly inappropriate name and I think that might be
misleading you. What the prefix "binary" means has had tens of thousands of
bytes of discussion on this newsgroup and it is just laying there waiting
for you to read. It only has to do with the representation of the '\n'
character. An 'a' is an 'a' whether the file it is in is called an ASCII
file or or it is called a binary. ASCII is often used (wrongly) as a
synonym for "text".
May 3 '06 #5
if you have a structure dumped into a file and you are going to read
the fields of the structure ,

simplest way is declare a variable of the strutcure .. erad into the
structure and print it like you would normally print any structure ..

if you have an array of structures , put this in a loop

if you have different structures(one after the other) , then you either
know the order of the structures or you can infer the type from the
file somehow .. use point 1 again case by case

May 3 '06 #6
On 2006-05-03, SandyMan <rs*********@gmail.com> wrote:
I have to do the following in C

I need to

A)Open a file (a binary file)
B) Read a field ( i know the offset and length)
C) Convert the value to Ascii /Hex
D)And then change the value of the field (which i have in Hex)
Why does it need to be in ascii/hex? why can't you change it in binary?
E) Convert it to Ascii
F)And put in the original file.

May 3 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: rbt | last post by:
Is there an easy way to exclude binary files (I'm working on Windows XP) from the file list returned by os.walk()? Also, when reading files and you're unsure as to whether or not they are ascii...
1
by: Ferran Foz | last post by:
Hello, I'm using ADODB.Stream to open a binary file on the server and write it down to the browser using Response.BinaryWrite. It's working fine, but i need to make some changes to the binary...
10
by: J. Campbell | last post by:
OK...I'm in the process of learning C++. In my old (non-portable) programming days, I made use of binary files a lot...not worrying about endian issues. I'm starting to understand why C++ makes...
9
by: Hemang Shah | last post by:
Hello fellow Coders! ok, I"m trying to write a very simple application in C#. (Yes its my first program) What I want to do is : 1) Open a binary file 2) Search this file for a particular...
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: ...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
5
by: bwv539 | last post by:
I have to output data into a binary file, that will contain data coming from a four channel measurement instrument. Since those data have to be read from another C program somewhere else, the...
1
by: Vic | last post by:
I have a c program which writes mac address entries onto a text file. Text file when opened in vim looks like this index mac 1 ^@^@^Q^@^@^A ascii of (00.00.11.00.00.01) 2 ^@^@^Q^@^@^B...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.