1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

data File in binary format

Discussion in 'Embedded' started by Yerry, Sep 19, 2009.

  1. Yerry

    Yerry Guest

    I have a data file which I want to convert into binary, so that I can
    use a EPROM programmer to burn the image of this data onto an ATMEL
    flash. Can I do it using fwrite in C?
    Yerry, Sep 19, 2009
    1. Advertisements

  2. Yerry

    Thad Smith Guest

    Yes. Open the file in binary mode.
    Thad Smith, Sep 19, 2009
    1. Advertisements

  3. Yerry

    D Yuniskis Guest

    Presumably the original file's contents are *not* in "binary"
    but, rather, Intel Hex, Motorola S Records, etc.?

    Are you sure your PROM Programmer doesn't already support the
    file in it's "native" format? (you don't mention where the
    original file came from...)
    D Yuniskis, Sep 20, 2009
  4. Yerry

    Yerry Guest

    My data is an array of integers in a C program. I converted (itoa)
    into char and then used fwrite to generate the binary file. However
    when i read the file in the buffer using the programmer sw, i see
    ascii values instead of my data values, why is that.
    Yerry, Sep 20, 2009
  5. Yerry

    Gilles Kohl Guest

    If your data is inside your program, you do not need to convert to
    ASCII via itoa prior to writing it to file. Can you post a sample of
    your data, the array declaration, and also how you are writing it to
    file using fwrite? (There's a few pitfalls here).

    Gilles Kohl, Sep 20, 2009
  6. Yerry

    Yerry Guest

    I converted into char because my data has to be written byte wise in
    the memory:
    Byte0 in memory should have first data word = 0x00
    Byte1 in memory should have second data word = 0x10
    Byte2..........data word = 0x20.
    Byte3 ......data word = 0x30.
    So i have to fill my data values within 1 byte (ofcourse the values
    are less than 255) and also have to swap the 4 bits (Data 04 goes in
    as 40 in memory). I thought as integer in C takes 16 bit, I can itoa
    my data into char and write as 8 bit words.
    Is there a way to write my data values without converting them into
    strings, and in 1 byte blocks.
    Yerry, Sep 21, 2009
  7. Yerry

    Gilles Kohl Guest

    My suggestion would be to convert your array in-place according to the
    described requirements (swapping nibbles), and then write out in one
    fell swoop, e.g. like so:

    #include <stdio.h>
    #include <stdlib.h>

    /* some sample data values */
    unsigned char values[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x80, 0xf0,
    0x0f, 0x42 };

    int main(int argc, char* argv[])
    int i;
    FILE *f;

    /* swap nibbles*/
    for(i = 0; i < sizeof(values); i++) {
    int swapped = ((int)values<<4) | ((int)values>>4);
    values = swapped & 0xff;

    /* open file for writing as binary */
    f = fopen("data.bin", "wb");

    if(f) {
    if(fwrite(values, sizeof(values), 1, f) != 1) {
    perror("Writing failed");
    else {
    perror("Open file failed");

    return 0;

    Gilles Kohl, Sep 21, 2009
  8. Yerry

    Frank Buss Guest

    itomartian sounds interesting. Can you post the source for it? Could be
    very useful for creating numbers for the Martian locale environment, which
    IBM has invented (see PDF file page 10, document page 8)


    The Unicode mapping looks funny. They have too much time at IBM.
    Frank Buss, Sep 21, 2009
  9. Yerry

    Frank Buss Guest

    It seems that you should learn more of the basics of C, e.g. what "char",
    "char*" and "int" means and what it means to cast, assign or itoa a
    variable of one type to the other type. But your question is not as easy to
    answer as it might seem. First you could try to use fprintf:

    fprintf(file, "%c", c);

    The pitfall for this concept is, that in Windows the byte 0x0a is written
    as two bytes, 0x0a 0x0d, when using printf. So next try is to use fputc:

    fputc(c, file);

    But it still converts 0x0a to two bytes, until you create the file in
    binary mode (at least on Windows, on Linux you don't have such problems)

    FILE* file = fopen(filename, "wb");

    An interesting note: fprintf converts 0x0a always to two bytes and ignores
    the file mode.

    Another idea would be to use fwrite, as Gilles demonstrated. You can use
    this for single chars, too. And yes, even fwrite converts 0x0a to two
    bytes, if not called with a file handle opened in binary mode. It would be
    a better world with 0x0a as line break, only, and no binary file mode.
    Frank Buss, Sep 21, 2009
  10. Yerry

    David Brown Guest

    I believe IBM also has a patent for faster-than-light communication,
    which could greatly improve the interplanetary telephone network.
    David Brown, Sep 21, 2009
  11. Yerry

    Frank Buss Guest

    I don't know, if it was IBM, but searching for such a patent shows this


    Citing from page 16 of the PDF file:

    | The present invention takes a transmission of energy, and instead of
    | sending it through normal time and space, it pokes a small hole into
    | another dimension, thus, sending the energy through a place which allows
    | transmission of energy to exceed the speed of light.

    That's cool! I would like to have such a device for transmitting the
    lottery numbers back in time :)
    Frank Buss, Sep 21, 2009
  12. Yerry

    Yerry Guest

    Hi Guys,
    Thank you for all the suggestions. Finally i used unsigned char to
    write my data into the a file and have successfully created a binary
    file as i wanted.
    Yerry, Sep 25, 2009
    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.