How the Dec to hex? And the node can show warn or errer information?

Dear All :

//show warn or error : The number is too large...
return null;
//Here is Dec to Hex...
return msg;

Thank u.

Hi jlian168. Here is a simple example.
What is your native language?
Are you a student?

[{"id":"aeecd730.670b08","type":"function","z":"283b54b6.9ab9ac","name":"","func":"if (msg.payload > 5000) {\n    node.warn (\"Number too large: \" + msg.payload);\n}\nelse {\n    msg.payload = msg.payload.toString(16);\n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":260,"wires":[["6379264e.fa09d"]]},{"id":"6379264e.fa09d","type":"debug","z":"283b54b6.9ab9ac","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":260,"wires":[]},{"id":"50f03a1.64ac2c4","type":"inject","z":"283b54b6.9ab9ac","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4999","payloadType":"num","x":190,"y":240,"wires":[["aeecd730.670b08"]]},{"id":"5e9e6062.a86f28","type":"inject","z":"283b54b6.9ab9ac","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5001","payloadType":"num","x":190,"y":280,"wires":[["aeecd730.670b08"]]}]

Dear jbudd :

  1. My native language is chinese.
  2. Thanks for ur code and I think ur code is to string not HEX,
    And I hope the warn can show in UI html But I dont know how...

Thank u very much.

Where do you want to see the warning? node red editor? As a popup on the dashboard or where?

What do you mean by hex if you do not mean a string with the hex characters? What do want to do with it?

This flow accepts a decimal number (as a string) on the UI.
It converts it to a number and then to (a string representation of) Hex.
eg it converts 423 to 1a7.
Surely the only way that Hex can be displayed is as a string?

Your English is much much better than my Chinese!
Are you a student?

[{"id":"aeecd730.670b08","type":"function","z":"283b54b6.9ab9ac","name":"","func":"if (msg.payload > 5000) {\n    msg.payload = \"Number too large: \" + msg.payload;\n}\nelse {\n    msg.payload = Number(msg.payload).toString(16);\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":260,"wires":[["9c16de2f.ff852"]]},{"id":"9c16de2f.ff852","type":"ui_text","z":"283b54b6.9ab9ac","group":"a6e358b.672ffa8","order":2,"width":"3","height":"1","name":"","label":"Hex is 0x","format":"{{msg.payload}}","layout":"row-left","x":440,"y":260,"wires":[]},{"id":"bfe545cb.c1af4","type":"ui_text_input","z":"283b54b6.9ab9ac","name":"","label":"Type a number ","tooltip":"","group":"a6e358b.672ffa8","order":1,"width":"3","height":"1","passthru":true,"mode":"text","delay":300,"topic":"topic","topicType":"msg","x":100,"y":260,"wires":[["aeecd730.670b08"]]},{"id":"a6e358b.672ffa8","type":"ui_group","name":"Demo","tab":"4e528085.a1bfa","order":1,"disp":true,"width":"20","collapse":false},{"id":"4e528085.a1bfa","type":"ui_tab","name":"Demo","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

Dear jbudd :

Im office worker.And I hv 3 question at Pics & below :

  1. The Dec to Hex is not correct?
  2. How to only show error information at text1?
  3. How can let switch when start is action at text?

Thank u.

You are over writting msg payload eg

let array=[0]
msg.payload = Number(msg.payload).toString(16);
array[1] = msg.payload << 8; //unsure why you are doing this
array[2]= msg.payload&0xff; // unsure why you are doing this
msg.payload =array;
return msg;



let array=[0]
array[0] = Number(msg.payload).toString(16);
array[1] = msg.payload << 8;
array[2]= msg.payload&0xff;
msg.payload = array;
return msg;


What exactly are you trying to get out of the function? Give us a sample input and tell us what output you want.

2 How to only show error information at text1?
Use a switch node (from the "function" section of the palette, not a UI switch) to send the msg to different UI elements depending on it's value or datatype.

3 How can let switch when start is action at text?
I do not understand the question.
But you can change the switch state either by clicking on it in the UI or by injecting a message into it.

[{"id":"25bac54e.c332e2","type":"ui_switch","z":"283b54b6.9ab9ac","name":"","label":"switch","tooltip":"","group":"a6e358b.672ffa8","order":5,"width":"2","height":"2","passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"","offvalue":"off","offvalueType":"str","officon":"","offcolor":"","animate":false,"x":510,"y":380,"wires":[["96469f52.d48928"]]},{"id":"d3165a0b.4d9f78","type":"inject","z":"283b54b6.9ab9ac","name":"Inject the switch ON payload","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":280,"y":360,"wires":[["25bac54e.c332e2"]]},{"id":"21397442.893e44","type":"inject","z":"283b54b6.9ab9ac","name":"Inject the switch OFF payload","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":280,"y":400,"wires":[["25bac54e.c332e2"]]},{"id":"96469f52.d48928","type":"debug","z":"283b54b6.9ab9ac","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":690,"y":380,"wires":[]},{"id":"eaf47ee1.d2862","type":"inject","z":"283b54b6.9ab9ac","name":"Anything else has no effect","props":[{"p":"topic","v":"0","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":270,"y":440,"wires":[["25bac54e.c332e2"]]},{"id":"a6e358b.672ffa8","type":"ui_group","name":"Demo","tab":"4e528085.a1bfa","order":1,"disp":true,"width":"20","collapse":false},{"id":"4e528085.a1bfa","type":"ui_tab","name":"Demo","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

Dear All Sir : Thanks for ur reply...

I hv another some question at Pics and below :
1.1. : Why show "1388" not 1388?
msg.payload = Number(msg.payload).toString(16);

1.2. : Why is 355328 not 13?

1.3. : Why is 108 not 88?
I want use a button, When I press the button then the function value
is send to debug window.
And now I press the button the is show "3c74b97f.ec90e6"! Why?
How to do Show it right?
Thank u.

Because you call toString then try to do bitshift operations on a string.

Dear Steve-Mcl :

So how to become Hex not Str?

Thank u.

Hexadecimal is just a number in base 16. The underlying number is the same.

So do all you math in decimal then ONLY when you want to display it should you convert to hexadecimal

As I asked before, tell us an example of an input and what you want to convert that to, and why you want to do it.

1 Like

Dear Colin :

  1. Input is Number(I think is Dec).
  2. In the function I want convert Dec to Hex.(But the resault is String!How to do become Hex?)
  3. Last I hope can press button then output show is Hex.(But button cant, How to do or use another node?)

Thank u.

So, converting to Hexadecimal will ALWAYS result in a string (because hexadecimal contains letters) - there is no way around this. BUT. Decimal and Hexadecimal (and octal and binary) are all just representations of the same number. My point is - stick with decimal for the maths THEN at the last moment, just before display, convert it to the format you wish to see.

This is what happens when you convert to hex string ...

This is what happens when you leave the number as decimal & convert at the end...

Can you please tell us an...

  • An example msg.payload input number (e.g. 4920)
  • what you expect to see at the end of your function (e.g. [ ???, ???, ??? ]


  • Arrays are zero based not 1 based
  • Why do you put the answers into an array anyway? They would be far better/easier in an object e.g...
    result = {};
    originalValue = msg.payload;
    result.original = originalValue;
    result.shifted = originalValue << 8;
    result.anded = originalValue & 0xFF;
    result.originalHex = originalValue.toString("16");
    result.shiftedHex = result.shifted.toString("16");
    result.andedHex = result.andedHex.toString("16");
    msg.payload = result;

Dear Steve-Mcl :

ok, I see, look at pic,

  1. so "1388" and"13 "and"88" are still String not Hex? if its String then how to become Hex?
  2. Last I hope can press button(maybe another node can be) then output to debug window, I dont know how to do?

Thank u.

'1388' IS hexadecimal equivalent of 4920


And as I said before

As I asked before...

(Google translation)

  • 示例 msg.payload 输入数字(例如 4920)
  • 您希望在函数结束时看到的内容(例如[ ???, ???, ??? ]

Dear Steve-Mcl :

ok, Sorry.


Now is ["1388","13","88"]
I hope is [1388,13,88]

Thank u.

So you can easily convert string to number using Number("1388")


If your incoming msg.payload is 4990 then the hex will be 137E

So you cannot turn 137E into a number