Coversion of Binary file to ASCII file using C.

Discussion in 'C Programming' started by SandyMan, May 3, 2006.

  1. SandyMan

    SandyMan Guest

    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
     
    SandyMan, May 3, 2006
    #1
    1. Advertising

  2. SandyMan

    Richard Bos Guest

    "SandyMan" <> 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
     
    Richard Bos, May 3, 2006
    #2
    1. Advertising

  3. SandyMan

    SandyMan Guest

    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
     
    SandyMan, May 3, 2006
    #3
  4. SandyMan

    SandyMan Guest

    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
     
    SandyMan, May 3, 2006
    #4
  5. SandyMan

    osmium Guest

    "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".
     
    osmium, May 3, 2006
    #5
  6. SandyMan

    thampan Guest

    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
     
    thampan, May 3, 2006
    #6
  7. SandyMan

    Jordan Abel Guest

    On 2006-05-03, SandyMan <> 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.
     
    Jordan Abel, May 3, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Schellens
    Replies:
    8
    Views:
    3,068
    John Harrison
    Jul 15, 2003
  2. TOXiC
    Replies:
    5
    Views:
    1,326
    TOXiC
    Jan 31, 2007
  3. vim
    Replies:
    68
    Views:
    2,279
  4. James O'Brien
    Replies:
    3
    Views:
    306
    Ben Morrow
    Mar 5, 2004
  5. Alextophi
    Replies:
    8
    Views:
    584
    Alan J. Flavell
    Dec 30, 2005
Loading...

Share This Page