MQTTX icon indicating copy to clipboard operation
MQTTX copied to clipboard

[Bug] Floats get Converted into INT when using CBOR or HEX

Open Zidail opened this issue 9 months ago • 1 comments

What did I do

Sent the following messages and received it:

  1. Sent out CBOR and received it as HEX
  2. Sent out as HEX and receive it as CBOR
CBOR to HEX

Sent out the following payload in CBOR:

// Payload
{
  "intvl": 60.0
}
HEX to CBOR

Sent out the following payload in HEX:

A165 696E 7476 6CF9 5380

What happened

Float values were converted from Float to Int. This happens specifically when the float ends with .0 or .00. So 10.0 gets converted to 10, however if its 10.1 it remains 10.1

Messages on both device and the client received the following:

CBOR to HEX

Received the following payload in HEX:

a165 696e 7476 6c18 3c
HEX to CBOR

Received the following payload in CBOR:

// Payload
{
  "intvl": 60
}

Expected

Floats should not be converted to Int if it ends with .0 or .00. Messages on both device and the client should receive the following:

CBOR to HEX

I should receive the following HEX payload:

A165 696E 7476 6CF9 5380
HEX to CBOR

I should receive the following CBOR payload:

// Payload
{
  "intvl": 60.0
}

Environment

  • OS: OS version: macOS Sequoia 15.4
  • MQTTX version: v1.11.1

More detail

Image

Zidail avatar Apr 10 '25 02:04 Zidail

Hello—sorry for the late reply! The core issue is how JavaScript represents all numbers as a single Number type, so CBOR’s canonical encoder sees 60.0 as an integer and emits 18 3C. This is CBOR/JS behavior, not an MQTTX bug.

To force 60.0 as a half‑precision float, try to send the raw HEX payload like this:

A1 65 69 6E 74 76 F9 53 80

ysfscream avatar Apr 20 '25 15:04 ysfscream

If no more issues, I’ll close it. Thanks for using it.

ysfscream avatar Jun 25 '25 10:06 ysfscream