EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

data File in binary format

Started by Yerry September 19, 2009
Hi
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 wrote:

> 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?
Yes. Open the file in binary mode. -- Thad
Thad Smith wrote:
> Yerry wrote: > >> 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? > > Yes. Open the file in binary mode.
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...)
> Presumably the original file's contents are *not* in "binary" > but, rather, Intel Hex, Motorola S Records, etc.?
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 wrote:
>> Presumably the original file's contents are *not* in "binary" >> but, rather, Intel Hex, Motorola S Records, etc.? > 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. >
You used "itoa" to convert the data into ASCII - that's why you see the data as ASCII. If you'd used the "itomartian" function instead, you'd see Martian values. If you are lucky and your host endianness is the same as your target endianness, just fwrite your integer array directly. If you need to change endianness or integer size, use second array matching the target sizes and fill it with data from your source before writing it out.
On Sun, 20 Sep 2009 10:28:20 -0700 (PDT), Yerry
<jerryken2009@gmail.com> wrote:

> >> Presumably the original file's contents are *not* in "binary" >> but, rather, Intel Hex, Motorola S Records, etc.? >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.
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). Regards, Gilles.
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.
On Sun, 20 Sep 2009 21:37:00 -0700 (PDT), Yerry
<jerryken2009@gmail.com> wrote:

>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.
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[i]<<4) | ((int)values[i]>>4); values[i] = 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"); } fclose(f); } else { perror("Open file failed"); } return 0; } Regards, Gilles.
David Brown wrote:

> You used "itoa" to convert the data into ASCII - that's why you see the > data as ASCII. If you'd used the "itomartian" function instead, you'd > see Martian values.
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) ftp://ftp.software.ibm.com/software/globalization/documents/Tivoli-GlobalVerificationTesting.pdf The Unicode mapping looks funny. They have too much time at IBM. -- Frank Buss, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
Yerry wrote:

> 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.
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, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de

The 2024 Embedded Online Conference