Coversion of Binary file to ASCII file using C.

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

  1. SandyMan

    SandyMan Guest


    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, 2006
    1. Advertisements

  2. SandyMan

    Richard Bos Guest

    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 Bos, May 3, 2006
    1. Advertisements

  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, May 3, 2006
  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


    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

    ret = fread(ptr,1,2,f);
    fclose(f); // close the filepointer

    Please tell me how to convert the binary data to Ascii/Hex data and
    also back to binary

    SandyMan, May 3, 2006
  5. SandyMan

    osmium Guest


    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

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

    Jordan Abel Guest

    Why does it need to be in ascii/hex? why can't you change it in binary?
    Jordan Abel, May 3, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.