Reply by Yerry September 24, 20092009-09-24
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 by Frank Buss September 21, 20092009-09-21
David Brown wrote:

> I believe IBM also has a patent for faster-than-light communication, > which could greatly improve the interplanetary telephone network.
I don't know, if it was IBM, but searching for such a patent shows this one: http://www.google.de/patents/about?id=csYDAAAAEBAJ&dq=6,025,810 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 by David Brown September 21, 20092009-09-21
Frank Buss wrote:
> 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. >
I believe IBM also has a patent for faster-than-light communication, which could greatly improve the interplanetary telephone network.
Reply by Frank Buss September 21, 20092009-09-21
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
Reply by Frank Buss September 21, 20092009-09-21
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
Reply by Gilles Kohl September 21, 20092009-09-21
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.
Reply by Yerry September 21, 20092009-09-21
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 by Gilles Kohl September 20, 20092009-09-20
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.
Reply by David Brown September 20, 20092009-09-20
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.
Reply by Yerry September 20, 20092009-09-20
> 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.