About variables

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 a union to assign a float and a byte array to the same memory location. Populate the float, and then copy the byte array to your array.

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 => 0x[color=blue]41240000[/color]

if you take that 41240000 and enter it in this online conversion tool you get back the 10.25

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++);

to write in position 1 to 4 of myarray[]

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
[![|500x326](http://img15.hostingpics.net/pics/447041result.png)](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++);


to write in position 1 to 4 of `myarray[]`

you explained perfectly. thanks :slight_smile:

:slight_smile: good :slight_smile:

J-M-L:
:slight_smile: good :slight_smile:

i am trying to convert the four bytes into float again on the receiver side but i’m getting error.

void float2Bytes(float val,byte* bytes_array){
  // Create union of shared memory space
  union {
    float float_variable;
    byte temp_array[4];
  } u;
  // Overite bytes of union with float variable
  u.float_variable = val;
  // Assign bytes to input array
  memcpy(bytes_array, u.temp_array, 4);
}


void setup() 
{
  Serial.begin(9600);
  
  float float_example = 1.123645;
  byte bytes[4];

  float2Bytes(float_example,&bytes[0]);

  float float_resurrect;
  for (int i=0; i<4 ; i++) 
  {
    float_resurrect <<= 8;
    float_resurrect |= (float)bytes[i] & 0xFF;
  }

  Serial.println(float_resurrect);
 
}
void loop()
{

}

float2Bytes function works perfectly by the way. it has no error. i checked it via your link :slight_smile:

J-M-L:
:slight_smile: good :slight_smile:

I did some research and do it with less and simple.

void setup() 
{
  Serial.begin(9600);
  
  float float_example = 1.123645;
  
  char myarray[sizeof(float)];
  
  memcpy(myarray, &float_example, sizeof float_example);

  float float_resurrect;
  
  memcpy(&float_resurrect, myarray, sizeof float_resurrect);    // receive data

  Serial.println(float_resurrect);
 
}
void loop()
{

}
    float_resurrect <<= 8;

What is this supposed to do ?