Problem to convert signed hexadecimal to float

Hello,
I am working on a node red project, and I want to read a voltage value.
The problem is that the value I'm reading is a signed hexadecimal value, which doesn't give me the right voltage value.
I would like to convert this signed hexadecimal value to a floating value.
Can someone help me, please ?

Are you reading these values via Modbus?

Can you post an example of flow that you use?

I use parseInt function :slight_smile:
image

Where do you get hex values from? Can you post full flow?

image

I use socketcan out to get the hex value and i converted it to decimal

Select the nodes which generate the issue and press export

https://nodered.org/docs/user-guide/editor/workspace/import-export

It is hard to understand the issue because it is possible to translate from hex to float.

[{"id":"1628789d76bcc9ed","type":"inject","z":"6ce20a4e7446e62d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"fefe","payloadType":"str","x":190,"y":860,"wires":[["77264eb7ae7f9bc0"]]},{"id":"77264eb7ae7f9bc0","type":"function","z":"6ce20a4e7446e62d","name":"hex to Float","func":"let decFromHex = parseInt(msg.payload, 16);\nmsg.payload = decFromHex /100\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":860,"wires":[["8ddee4ea1572836a"]]},{"id":"8ddee4ea1572836a","type":"debug","z":"6ce20a4e7446e62d","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":860,"wires":[]}]

I haven't worked with socketcan but you are probably receiving a buffer of hex. In this case you should use a converter:

This might help you:

there are plenty of examples in nodered forum also

I use node-red-contrib-buffer-parser for convert signed hexadecimal from PLC.

How do you use the parser buffer. I don't understand how it works ?

There are built in examples (use CTRL+I in node-red to access examples)

There is also built in help (on the Node-red sidebar) and the readme is quite informative.

Lastly, there are tonnes of working examples around the forum where it has solved the same problem you have.

Can you gives the way on the forum to check the answers about my problem ?

Use the search facility (Magnifying Glass) Top Right Corner.

Please don't forget, we are all Volunteers here.

Here is a post from your other thread: Node Red socketcan output frame received problem - #5 by Steve-Mcl

Here is a post where a buffer is converted to float: Modbus TCP float to value - #35 by Steve-Mcl

Hello,
I would like to retrieve a current value.
When I debug, I get the hexadecimal signed value FC7C which corresponds in decimal to -900:
image

The problem is that when I want to convert this hex value to decimal using the function below, I get as decimal value 646,37 instead of -9 A.

I don't understand how to do this, does anyone have a solution?
Thanks

If you use buffer parser to parse your values like I showed you in previous post, simply select the int16 option and you will get the correct signed value

Thanks a lot, I managed to solve the problem :
image

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