Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Coversion of Binary file to ASCII file using C.

Reply
Thread Tools

Coversion of Binary file to ASCII file using C.

 
 
SandyMan
Guest
Posts: n/a
 
      05-03-2006
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

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      05-03-2006
"SandyMan" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
SandyMan
Guest
Posts: n/a
 
      05-03-2006
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

 
Reply With Quote
 
SandyMan
Guest
Posts: n/a
 
      05-03-2006
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

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      05-03-2006
"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".


 
Reply With Quote
 
thampan
Guest
Posts: n/a
 
      05-03-2006
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

 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      05-03-2006
On 2006-05-03, SandyMan <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex with ASCII and non-ASCII chars TOXiC Python 5 01-31-2007 04:48 PM
differance between binary file and ascii file vim C Programming 68 05-27-2006 08:47 PM
[FR/EN] how to convert the characters ASCII(0-255) to ASCII(0-127) Alextophi Perl Misc 8 12-30-2005 10:43 AM
routine/module to translate microsoft extended ascii to plain ascii James O'Brien Perl Misc 3 03-05-2004 04:33 PM
Re: ostreams, ios::binary, endian, mixed binary-ascii Marc Schellens C++ 8 07-15-2003 12:27 PM



Advertisments