data File in binary format

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?

Reply to
Yerry
Loading thread data ...

Yes. Open the file in binary mode.

--
Thad
Reply to
Thad Smith

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

Reply to
D Yuniskis

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.

Reply to
Yerry

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.

Reply to
David Brown

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.

Reply to
Gilles Kohl

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.

Reply to
Yerry

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 #include

/* 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); 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.

Reply to
Gilles Kohl

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
Reply to
Frank Buss

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
Reply to
Frank Buss

ftp://ftp.software.ibm.com/software/globalization/documents/Tivoli-GlobalVerificationTesting.pdf

I believe IBM also has a patent for faster-than-light communication, which could greatly improve the interplanetary telephone network.

Reply to
David Brown

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

formatting link

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

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.

Reply to
Yerry

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.