Object to float value

HI, im trying to get my head round javascript still, ground to a halt again, and it will probably be a really easy thing to resolve, ive tried googling and not had any luck.
I have a serial device that is send battery data, certain parts are binary data others are ascii depenedant on the message and the address, this i have sorted and i have the appropiate channels switching to there resepctive channels.
now to the point, the payload im getting out is a

buffer  
 [73,81,70,80,84]

, now this is showing as decimal numbers, but if i open the array they are HEX

buffer [5]
0:0x49
1:0x51
2:0x46
3:0x50
4:0x54

so im trying to convert this buffer into a float that should be 13.26, but if i try to use

msg.payload = Buffer.from(msg.payload,"hex").toString()
return msg;

I get "IQFPT" , which is the decimal values rather than the hex in the buffer.
like i siad im probably be a dumb arse and its blatantly obvious , but after the fortnight ive had im amazed i can even type.
Thanks in advance

It looks like a string. Try. msg.payload = parseFloat(msg.payload,toString());

1 Like

For a source buffer of [73,81,70,80,84] you dont specify HEX in the from() (thats for when the data you are providing is a HEX string.

Here are some options...

var buf = Buffer.from([73,81,70,80,84])

buf.readFloatBE(0)     //--> 857189
buf.readFloatLE(0)     //--> 13308863488
buf.toString()         //'IQFPT'
buf.toString("hex")    //--> "4951465054"

NOTE: a float is 4 bytes (32 bit) - your data has 5 bytes.

1 Like

Thanks guys , turns out the values i was given are decimal ascii numbers not the hex values so my duino was sending " IQFPT".
:flushed:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.