Coversion of Binary file to ASCII file using C.

S

SandyMan

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
 
R

Richard Bos

SandyMan said:
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
 
S

SandyMan

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
 
S

SandyMan

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
 
O

osmium

SandyMan said:
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".
 
T

thampan

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
 
J

Jordan Abel

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top