sprintf for unsigned long to padded HEX

Hi everyone.

Im starting to loose my mind here.

  char tx_data[9];
  uint32_t uptime = millis();
  Serial.print("Uptime: ");
  Serial.println(uptime, HEX);
  Serial.println(sizeof(millis()));
  sprintf(tx_data, "%08X", uptime);
  Serial.print(">");
  Serial.print(tx_data);
  Serial.println("<");
Uptime: 7F581
4
>0000F581<

So, where am I loosing (at least) the second MSB (0x07) of the 'uptime' ulong?
Its somewhere in the sprintf function but Im not able to find any reference for sprintf having this type of limitation (to two LSB bytes) in Arduino.

Thanks,
Marcel

The X format specifier is for what data type? Not long, signed or unsigned.

You have to signal the long argument to sprintf, use

sprintf(tx_data, "%08lX", uptime);

See documentation.

Whandall:
You have to signal the long argument to sprintf, use

sprintf(tx_data, "%08lX", uptime);

See documentation.

Now that is something that is difficult to spot!

Thanks Whandall!

The excempt from the linked docs:

An optional l or h length modifier, that specifies that the argument for the d, i, o, u, x, or X conversion is a "long int" rather than int. The h is ignored, as "short int" is equivalent to int.