user61361: I am trying to convert a float value to a 4-byte hexadecimal value.
For example my input is: 58.5. My expected output is: 426A0000 or like 0x42 0x6A 0x00 0x00.
My program is:
void setup() {
  Serial.begin(9600);
  float z = 128;
  unsigned i;
  unsigned char *chpt;
  chpt = (unsigned char *)&z;
  Serial.print("Float Value             : ");
  Serial.println(z);
  Serial.print("4 Byte hexadecimal Value: ");
  for (i = 0; i < sizeof(z); i++) {
    Serial.print(chpt[i],HEX);
    //Serial.print(" ");
  }
  Serial.println();
}
void loop() {
}

My current output is:
Float Value             : 128.00
4 Byte hexadecimal Value: 00043

user23644: This value looks pretty correct to me. It is stored in little endian thou.
0x43000000 is the actual value.
Serial.print(x, HEX) truncates leading zeros. so 00 gets 0
So your output is 0 0 0 43 which corresponds to the little endian representation of the above.
Make yourself familiar with the floating point representation IEEE-754.
Also try the online FoatConverter.
Try the following:
char floatString[9];
sprintf(floatString, "%02X%02X%02x%02x", chpt[3], chpt[2], chpt[1], chpt[0]);
floatString[8] = '\0';
Serial.print(floatString);


61361: <p>I am trying to convert a float value to a 4-byte hexadecimal value.</p>

<p>For example my input is: 58.5. My expected output is: 426A0000 or like 0x42 0x6A 0x00 0x00.</p>

<p>My program is:</p>

<pre><code>void setup() {
  Serial.begin(9600);
  float z = 128;
  unsigned i;
  unsigned char *chpt;
  chpt = (unsigned char *)&amp;z;
  Serial.print("Float Value             : ");
  Serial.println(z);
  Serial.print("4 Byte hexadecimal Value: ");
  for (i = 0; i &lt; sizeof(z); i++) {
    Serial.print(chpt[i],HEX);
    //Serial.print(" ");
  }
  Serial.println();
}
void loop() {
}
</code></pre>

<p>My current output is:</p>

<p><code>Float Value             : 128.00</code></p>

<p><code>4 Byte hexadecimal Value: 00043</code></p>
 23644: <p>This value looks pretty correct to me. It is stored in little endian thou.</p>

<p>0x43000000 is the actual value.</p>

<p><code>Serial.print(x, HEX)</code> truncates leading zeros. so <code>00</code> gets <code>0</code>
So your output is <code>0 0 0 43</code> which corresponds to the little endian representation of the above.</p>

<p>Make yourself familiar with the floating point representation IEEE-754.</p>

<p>Also try the online <a href="https://www.h-schmidt.net/FloatConverter/IEEE754.html" rel="nofollow noreferrer">FoatConverter</a>.</p>

<p>Try the following:</p>

<pre><code>char floatString[9];
sprintf(floatString, "%02X%02X%02x%02x", chpt[3], chpt[2], chpt[1], chpt[0]);
floatString[8] = '\0';
Serial.print(floatString);
</code></pre>
