Hello all,
I need to send an integer as 8 bit string to a flash memory. the integer will be 24 bit long . Anyone has any suggestion on how I can send the 24 bit integer as three 8 bit bytes in C code ?
Thanks in advance.
Assume the 24bit integer is stored in a 32bit variable called aa.
unsigned long aa;
unsigned char x,y,z;
x = (unsigned char)(aa / 0x10000); // the MSB 8 bits
send_to_flash(x);
y = (unsigned char)((aa % 0x10000) / 0x100); // the middle 8 bits
send_to_flash;
z = (unsigned char)(aa % 0x100); // the LSB 8 bits
send_to_flash(z);
Ther are other methods also. You can use the shift operator or you can use a union. The above method is not the most efficient. But it is easy to comprehend. The most efficient method is to use a union.
unsigned long aa;
unsigned char *tosend=(unsigned char*)&aa;
send_to_flash(tosend[0]);
send_to_flash(tosend[1]);
send_to_flash(tosend[2]);
Note: you must consider the endian problem and the order in which you send to flash. There is another probability:
send_to_flash(tosend[2]);
send_to_flash(tosend[1]);
send_to_flash(tosend[0]);