Advertisement
RuiViana

Float_to_Array_to_Float

Oct 1st, 2018
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1.   //------------------------------------------------  
  2. void setup() {
  3.   Serial.begin(9600);
  4.   //------------------------------------------------   Float to array
  5.   float z = 22.55;
  6.   unsigned char *chpt;
  7.   chpt = (unsigned char *)&z;
  8.   Serial.print("float  : ");
  9.   Serial.println(z);
  10.   Serial.print("  Hex  : ");
  11.   for (int i = 0; i < sizeof(z); i++) {
  12.     Serial.print("0x");
  13.     Serial.print(chpt[i], HEX);
  14.     Serial.print(" ");
  15.   }
  16.   Serial.println();
  17.   //------------------------------------------------   Array to float
  18.   union {
  19.     char c[4];
  20.     float f;
  21.   } u;
  22.   for (int i = 3; i > -1; i--)
  23.   {
  24.     u.c[i] = chpt[i];
  25.   }
  26.   Serial.print("float  : "); Serial.println( u.f);
  27. }
  28.   //------------------------------------------------  
  29. void loop() {
  30.   // put your main code here, to run repeatedly:
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement