Coversion of Binary file to ASCII file using C.

Coversion of Binary file to ASCII file using C.

Post by SandyMa » Thu, 04 May 2006 15:08:33


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
 
 
 

Coversion of Binary file to ASCII file using C.

Post by rlb » Thu, 04 May 2006 15:35:25


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

 
 
 

Coversion of Binary file to ASCII file using C.

Post by SandyMa » Thu, 04 May 2006 16:03:29

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
 
 
 

Coversion of Binary file to ASCII file using C.

Post by SandyMa » Thu, 04 May 2006 16:25:08

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
 
 
 

Coversion of Binary file to ASCII file using C.

Post by osmiu » Thu, 04 May 2006 17:30:23

"SandyMan" writes:

<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".
 
 
 

Coversion of Binary file to ASCII file using C.

Post by thampa » Thu, 04 May 2006 18:28:15

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
 
 
 

Coversion of Binary file to ASCII file using C.

Post by Jordan Abe » Fri, 05 May 2006 01:57:56


Why does it need to be in ascii/hex? why can't you change it in binary?