Another ask for help with convert numbers

Sorry, but I need to ask for help again....

Now I want to convert number to hex, but values are weird.
For example:

I've got -1, I want to convert it to FF,
Next, I've got -2, this should be FE
.
.
.
Finally I want to end at -20, and it should be EC

I know, it's weird, but device that I working on is abnormal :upside_down_face:

My nickname obliges me to ask such questions :crazy_face:

In a function node, assuming msg.payload has the value (-1, -2, -3 etc) create an array of the codes

let codes = ["FF", "FE", "FD",..."EE"];
let mynum = msg.payload;
let code = codes[((mynum*-1)-1)];
let msg.payload = code;

so you have an array of the codes but the numbers are negative and the first entry in the array is offset zero. If you multiply the number by -1 it will become a positive number, subtract 1 from it and you should have the lookup value to get the code you want.

1 Like

You can do all of this with the buffer-maker node in the buffer-parser package.

1 Like

@zenofmud @Steve-Mcl

Thanks for your replies, now I think I will manage with that :slight_smile:

slight bug in my code, the 'let msg.payload = code' should not have the 'let'

let codes = ["FF", "FE", "FD"]
let mynum = msg.payload;
let code = codes[((mynum * -1) - 1)]
msg.payload = code;
return msg;

you also could use abs() so the code would be:

let codes = ["FF", "FE", "FD"]
let mynum = msg.payload;
let code = codes[Math.abs(mynum) - 1]
msg.payload = code;
return msg;

That is not weird at all. Suppose you have an 8 bit byte with the value 0, so, in binary that is 0000 0000. Now subtract one from that, then the result is 1111 1111, with a carry off the front. Now if you add 1 back on again you get back to 0000 0000. So 1111 1111 or FF is in fact the normal representation of -1 in an 8 bit byte.

In order to do what (I think) you want it is just necessary to logical AND it with 0xFF, so if the value is in msg.payload you can do
msg.payload = msg.payload & 0xFF
That may be exactly what you want or it may not, it depends on what you want to do with it. If you want it as a number, for sending to Modbus for example then the result is what you want. If you want it as a string then you can use
msg.payload = (msg.payload & 0xff).toString(16)

However, as @Steve-Mcl said, the buffer parser node can do this for you, particularly if you need a number of such values in an array for sending to Modbus, for example.

Thank you for reply

Yes, the buffer parser node could help with that, but in my case this function solves all problems (so far) :slight_smile:

msg.payload = (msg.payload & 0xff).toString(16)
msg.payload = msg.payload.toUpperCase();
return msg;

As a matter of interest, what are you going to do with the data that needs it in that form?

Generally this is a HVAC controller, but for great simplicity I can tell you it uses modified Modbus RTU transmission (I think :upside_down_face: )

This thread that I created is responsible for correction of outside temperature and positive values of correction are presented as 00-0F (hex) for 0°C to +3°C.
Negative values are shown as FF-F1(hex) [decreasing] for -0,2°C to -3°C.

Hope I've explained it thoroughly :slight_smile:

If you want to send it via modbus I would expect it to need a buffer or array of binary values, not strings.

I wrote I think it using modified Modbus RTU protocol, there are some similarites (a little), but the entire transmission is carried out by strings

OK. In that case you are right, obviously.

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