Convert hex string to float

Hi,

This is probably pretty rudimentary but I can't seem to get it right. I have a JSON object containing a HEX value, im trying to change it to a float/decimal it's a temperature reading.
image

What format is it in? There are many ways of representing float values. If you don't know the format where did it come from?

Hi,
Its a float value from a tm35 sensor converted to hex and sent as a json message to node red,

Hi

Without context its fairly meaningless.

For example is it a scaled value?
Is it in engineering units or raw from the sensor?
What number are you expecting 0x32352e3339 (dec 215640585017) to represent in Celsius?

Also how has it been converted to hex (and why)? Why not just send the float value?

Hi,
The temp sensor is value is say 25.39 as a float. The temp is converted to ascii symbol and then to hex, 2(32), 5(35), .(2e), 3(33), 9(39) giving 32352e3339 this is send as raw to the sigfox backend, watson IOT, and to node red as a JSON object. I'm trying now to get the JSON object back to the float 25.39

That would have been useful info for the first post!
You can use a function node

e.g.

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

Some proof...
image

@steifanc to explain why your question was not clear, if, for example, you were getting float values via modbus from some device then they might well be IEEE754 encoded which is quite different, and there are other possibilities too.

Thanks all ,
the function is throwing up a type error , ill need to play with it,
image

show us the debug output from before the function node and the function node code.

I suspect you are not getting the right property from the msg or its a number not a string.

All are easy fixes.

Hi ,

Function node
image

Debug node
image

so where is the part that converts from hex to ascii?

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

In particular - I need to see what you are using inside the Buffer.from function and I need to see what it contains (a string? a number?)

The error is pretty clear (first arg must be a string)

Also in that function you are referencing msg.payload.d.value but the debug output you show has msg.payload as a string not an object with attribute d
I think it would be worth your while reading the node red docs page Working with Messages to get some background information on how to handle messages.

Hi ,
I don`t have a buffer function.
I have a message coming from watson IOT , below and im trying to change the message back to the float value

image

So you basically ignored the solution provided...

Post your flow and I'll fix it for you

Hi ,
I did try the function you provided and i got the Type error.

flow below

[{"id":"c81a5aed.869158","type":"debug","z":"4dc36eba.fc14d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1110,"y":540,"wires":[]},{"id":"e94d6a10.4b4fc8","type":"function","z":"4dc36eba.fc14d","name":"","func":"msg.payload = Buffer.from(msg.d,\"hex\").toString()\nreturn msg;","outputs":1,"noerr":0,"x":910,"y":460,"wires":[["c8bb370a.08bef8"]]},{"id":"1a909fde.c739d","type":"function","z":"4dc36eba.fc14d","name":"","func":"p=msg.payload;\nnode.log(typeof p);\nmsg.payload=p.d.value;\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":460,"wires":[["e94d6a10.4b4fc8","c81a5aed.869158"]]},{"id":"c8bb370a.08bef8","type":"debug","z":"4dc36eba.fc14d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1090,"y":460,"wires":[]},{"id":"188f638b.05a33c","type":"ibmiot in","z":"4dc36eba.fc14d","authentication":"apiKey","apiKey":"8b357272.1f43","inputType":"evt","logicalInterface":"","ruleId":"","deviceId":"36C664","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"value","service":"registered","allDevices":false,"allApplications":"","allDeviceTypes":true,"allLogicalInterfaces":"","allEvents":true,"allCommands":"","allFormats":false,"qos":0,"x":590,"y":460,"wires":[["1a909fde.c739d"]]},{"id":"8b357272.1f43","type":"ibmiot","z":"","name":"Test_","keepalive":"60","serverName":"","cleansession":true,"appId":"","shared":false}]

[{"id":"46f2e36d.79332c","type":"debug","z":"8712f97c.b302e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1110,"y":100,"wires":[]},{"id":"5c3a833c.59b90c","type":"function","z":"8712f97c.b302e8","name":"","func":"msg.payload = Buffer.from(msg.payload.d,\"hex\").toString()\nreturn msg;","outputs":1,"noerr":0,"x":1110,"y":160,"wires":[["b874a36c.dd7ef"]]},{"id":"b874a36c.dd7ef","type":"debug","z":"8712f97c.b302e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1290,"y":160,"wires":[]},{"id":"f1adb05f.c5deb","type":"inject","z":"8712f97c.b302e8","name":"","topic":"fake ibmiot","payload":"{\"d\":\"32352e3339\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":900,"y":100,"wires":[["5c3a833c.59b90c","46f2e36d.79332c"]]},{"id":"1cf837b5.8c22b8","type":"ibmiot in","z":"8712f97c.b302e8","authentication":"apiKey","apiKey":"8b357272.1f43","inputType":"evt","logicalInterface":"","ruleId":"","deviceId":"36C664","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"value","service":"registered","allDevices":false,"allApplications":"","allDeviceTypes":true,"allLogicalInterfaces":"","allEvents":true,"allCommands":"","allFormats":false,"qos":0,"x":870,"y":160,"wires":[["5c3a833c.59b90c"]]}]

ignore the missing node (I dont use that node)

Looking at the debug it seems to be msg.payload.d.value.

@steifanc have a read through the node red docs page Working with Messages. Understanding that I think you would have been able to modify @Steve-Mcl's suggestion to match your data.

Good spot.

Updated the demo flow to use msg.payload.d.value...

[{"id":"46f2e36d.79332c","type":"debug","z":"8712f97c.b302e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1110,"y":100,"wires":[]},{"id":"5c3a833c.59b90c","type":"function","z":"8712f97c.b302e8","name":"","func":"msg.payload = Buffer.from(msg.payload.d.value,\"hex\").toString()\nreturn msg;","outputs":1,"noerr":0,"x":1110,"y":160,"wires":[["b874a36c.dd7ef"]]},{"id":"b874a36c.dd7ef","type":"debug","z":"8712f97c.b302e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1290,"y":160,"wires":[]},{"id":"f1adb05f.c5deb","type":"inject","z":"8712f97c.b302e8","name":"","topic":"fake ibmiot","payload":"{\"d\":{\"value\":\"32352e3339\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":830,"y":100,"wires":[["5c3a833c.59b90c","46f2e36d.79332c"]]},{"id":"1cf837b5.8c22b8","type":"ibmiot in","z":"8712f97c.b302e8","authentication":"apiKey","apiKey":"8b357272.1f43","inputType":"evt","logicalInterface":"","ruleId":"","deviceId":"36C664","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"value","service":"registered","allDevices":false,"allApplications":"","allDeviceTypes":true,"allLogicalInterfaces":"","allEvents":true,"allCommands":"","allFormats":false,"qos":0,"x":870,"y":160,"wires":[["5c3a833c.59b90c"]]}]