Hi.
Im trying to communicate with modules using char arrays (modules are not relevant).
How can i insert floating points to my array.
for example:
char myarray[20];
myarray[0] = ....; (reserved)
myarray[1] = first byte of my floating point number 1;
myarray[2] = second byte of my floating point number 1;
myarray[3] = third byte of my floating point number 1;
myarray[4] = fourth byte of my floating point number 1;
myarray[5] = first byte of my floating point number 2;
myarray[6] = second byte of my floating point number 2;
myarray[7] = third byte of my floating point number 2;
myarray[8] = fourth byte of my floating point number 2;
when i receive this message i will need to seperate them into one byte and two floating numbers too.
thanks for your answers from now.
you can use dtostrf() and pass &(myarray[1]) as the place where you want to start to write
Oops - misread your intent if you really want just the bytes representing the float. Paul's approach works. otherwise just casting a pointer to your float as a char *
here is an example
void setup() {
char myarray[20];
float t1 = 10.25;
char * fptr;
Serial.begin(115200);
myarray[0] = 'X';
fptr = (char*) (&t1);
for (int i = 4; i > 0; --i) myarray[i] = *(fptr++); // little endian, data in opposite direction
Serial.print("t1 = ");
Serial.println(t1);
Serial.print("char array => 0x");
for (int i = 1; i <= 4; i++) {
if (myarray[i] < 10) Serial.print('0');
Serial.print(myarray[i], HEX);
}
}
void loop() {}
Note that I had to reverse the order of the bytes as Arduino is Little endian so bytes are written in the reverse order as a human would read them (least significant byte first), which was not convenient to paste in the web site.
for your code you don't want to do this, you would change the for loop to be:
fptr = (char*) (&t1);
for (int i = 1; i <= 4; i++) myarray[i] = *(fptr++);
J-M-L: you can use dtostrf() and pass &(myarray[1]) as the place where you want to start to write
Oops - misread your intent if you really want just the bytes representing the float. Paul's approach works. otherwise just casting a pointer to your float as a char *
here is an example
void setup() {
char myarray[20];
float t1 = 10.25;
char * fptr;
Serial.begin(115200);
myarray[0] = 'X';
fptr = (char*) (&t1);
for (int i = 4; i > 0; --i) myarray[i] = *(fptr++); // little endian, data in opposite direction
Serial.print("t1 = ");
Serial.println(t1);
Serial.print("char array => 0x");
for (int i = 1; i <= 4; i++) {
if (myarray[i] < 10) Serial.print('0');
Serial.print(myarray[i], HEX);
}
}
void loop() {}
that will print out
t1 = 10.25
char array => 0x41240000
if you take that 41240000 and enter it in [this online conversion tool](http://babbage.cs.qc.cuny.edu/IEEE-754.old/32bit.html) you get back the 10.25
[](http://img15.hostingpics.net/pics/447041result.png)
Note that I had to reverse the order of the bytes as Arduino is Little endian so bytes are written in the reverse order as a human would read them (least significant byte first), which was not convenient to paste in the web site.
for your code you don't want to do this, you would change the for loop to be:
fptr = (char*) (&t1);
for (int i = 1; i <= 4; i++) myarray[i] = *(fptr++);