Converting Decimal to Hex

Hi there

I'm trying to convert Decimal to Hex with raw code as a challenge someone gave me but im struggling to figure out why the the payload that's shown in the debug node is "undefined".
Thank you in advance for the help

Kind Regards
Apple

[{"id":"d3a188d4.8d7a5","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"b4480307.eea488","type":"ui_tab","name":"Text","icon":"dashboard","disabled":false,"hidden":false},{"id":"74716359.e24674","type":"ui_base","theme":{"name":"theme-light","lightTheme":{"default":"#0094CE","baseColor":"#0094CE","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":false},"darkTheme":{"default":"#097479","baseColor":"#097479","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":false},"customTheme":{"name":"Untitled Theme 1","default":"#4B7930","baseColor":"#4B7930","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"},"themeState":{"base-color":{"default":"#0094CE","value":"#0094CE","edited":false},"page-titlebar-backgroundColor":{"value":"#0094CE","edited":false},"page-backgroundColor":{"value":"#fafafa","edited":false},"page-sidebar-backgroundColor":{"value":"#ffffff","edited":false},"group-textColor":{"value":"#1bbfff","edited":false},"group-borderColor":{"value":"#ffffff","edited":false},"group-backgroundColor":{"value":"#ffffff","edited":false},"widget-textColor":{"value":"#111111","edited":false},"widget-backgroundColor":{"value":"#0094ce","edited":false},"widget-borderColor":{"value":"#ffffff","edited":false},"base-font":{"value":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"}},"angularTheme":{"primary":"indigo","accents":"blue","warn":"red","background":"grey","palette":"light"}},"site":{"name":"Node-RED Dashboard","hideToolbar":"false","allowSwipe":"false","lockMenu":"false","allowTempTheme":"true","dateFormat":"DD/MM/YYYY","sizes":{"sx":48,"sy":48,"gx":6,"gy":6,"cx":6,"cy":6,"px":0,"py":0}}},{"id":"47c654c9.b93774","type":"ui_group","name":"TextInput","tab":"b4480307.eea488","order":1,"disp":true,"width":"6","collapse":false},{"id":"f907f557.afb4e8","type":"ui_tab","name":"IP","icon":"dashboard","disabled":false,"hidden":false},{"id":"23ae81f3.a18136","type":"ui_group","name":"IPInput","tab":"f907f557.afb4e8","order":1,"disp":true,"width":"6","collapse":false},{"id":"c777dfe0.1979c","type":"inject","z":"d3a188d4.8d7a5","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"123","payloadType":"str","x":110,"y":100,"wires":[["4cd0ed56.445ecc"]]},{"id":"4cd0ed56.445ecc","type":"function","z":"d3a188d4.8d7a5","name":"","func":"var val = msg.payload;\nvar res;\nvar numval =\"\";\nvar hexval;\nvar endval;\nvar Hex = new Array(16);\nHex[0] = 0;\nHex[1] = 1;\nHex[2] = 2;\nHex[3] = 3;\nHex[4] = 4;\nHex[5] = 5;\nHex[6] = 6;\nHex[7] = 7;\nHex[8] = 8;\nHex[9] = 9;\nHex[10] = \"A\";\nHex[11] = \"B\";\nHex[12] = \"C\";\nHex[13] = \"D\";\nHex[14] = \"E\";\nHex[15] = \"F\";\nwhile(res > 0)\n{\n    res = val%16;\n    for(i=0;i<10;i+1)\n    {\n        if(res == Hex[i])\n        {\n            numval = res + numval;\n        }else if (res<9>16)\n        {\n            hexval = Hex[res];\n       }\n   }\n    val = val/16;\n}\nendval = numval+hexval;\nmsg.payload = {payload:endval};\n\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":260,"y":140,"wires":[[]]},{"id":"539cd750.5ac72","type":"debug","z":"d3a188d4.8d7a5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":100,"wires":[]}]

Hi,

If your payload is a number you can use in function:

msg.payload = msg.payload.toString(16);
return msg;

if your payload is a string representing a number you can use:

msg.payload = toHex(msg.payload)
return msg;

function toHex(d) {
    return  ("0"+(Number(d).toString(16))).slice(-2).toUpperCase()
}

Example:

[{"id":"c777dfe0.1979c","type":"inject","z":"d3a188d4.8d7a5","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"123","payloadType":"str","x":210,"y":360,"wires":[["4f1cac27.3a7b8c"]]},{"id":"539cd750.5ac72","type":"debug","z":"d3a188d4.8d7a5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":340,"wires":[]},{"id":"4f1cac27.3a7b8c","type":"function","z":"d3a188d4.8d7a5","name":"","func":"msg.payload = toHex(msg.payload)\nreturn msg;\n\nfunction toHex(d) {\n    return  (\"0\"+(Number(d).toString(16))).slice(-2).toUpperCase()\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":360,"wires":[["539cd750.5ac72"]]},{"id":"cd5ff797.3337","type":"function","z":"d3a188d4.8d7a5","name":"","func":"msg.payload = msg.payload.toString(16);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":440,"wires":[["539cd750.5ac72"]]},{"id":"1e60915e.9e78d7","type":"inject","z":"d3a188d4.8d7a5","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"123","payloadType":"num","x":210,"y":440,"wires":[["cd5ff797.3337"]]}]
1 Like

Thanks for the help but the examples you've given me already does the conversion. I'm struggling to figure out why my code is read as undefined. Based off of what you've told me i think my payload need to be changed but when testing the individual variables they also don't seem to be receiving any values which is more confusing now.

Since this is a challenge, let me give you a debugging idea. You can add node.warn statements into your functions node to look at what variables look like at different points

node.warn("x=" + x);

1 Like

this is very useful thanks but do you perhaps know why it wouldn't work in a while or if statement?

Are you saying the node warn doesn't work for you?? You did use your variables and not x...right???

Yes sir I used my own variables. It works perfectly outside "while" or "if" statements but as soon as i use them inside those statements it doesn't show in the debug window

That would suggest that the conditions for entering the while or if block are not being met (so the warn function isn't called). Try outputting the variables involved in the conditions before those statements.

1 Like

Also show us the code for your function and what the input to the function is (as shown in a debug node).

[{"id":"d3a188d4.8d7a5","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"62f0c6d6.6621a8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"b4480307.eea488","type":"ui_tab","name":"Text","icon":"dashboard","disabled":false,"hidden":false},{"id":"74716359.e24674","type":"ui_base","theme":{"name":"theme-light","lightTheme":{"default":"#0094CE","baseColor":"#0094CE","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":false},"darkTheme":{"default":"#097479","baseColor":"#097479","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif","edited":false},"customTheme":{"name":"Untitled Theme 1","default":"#4B7930","baseColor":"#4B7930","baseFont":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"},"themeState":{"base-color":{"default":"#0094CE","value":"#0094CE","edited":false},"page-titlebar-backgroundColor":{"value":"#0094CE","edited":false},"page-backgroundColor":{"value":"#fafafa","edited":false},"page-sidebar-backgroundColor":{"value":"#ffffff","edited":false},"group-textColor":{"value":"#1bbfff","edited":false},"group-borderColor":{"value":"#ffffff","edited":false},"group-backgroundColor":{"value":"#ffffff","edited":false},"widget-textColor":{"value":"#111111","edited":false},"widget-backgroundColor":{"value":"#0094ce","edited":false},"widget-borderColor":{"value":"#ffffff","edited":false},"base-font":{"value":"-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen-Sans,Ubuntu,Cantarell,Helvetica Neue,sans-serif"}},"angularTheme":{"primary":"indigo","accents":"blue","warn":"red","background":"grey","palette":"light"}},"site":{"name":"Node-RED Dashboard","hideToolbar":"false","allowSwipe":"false","lockMenu":"false","allowTempTheme":"true","dateFormat":"DD/MM/YYYY","sizes":{"sx":48,"sy":48,"gx":6,"gy":6,"cx":6,"cy":6,"px":0,"py":0}}},{"id":"47c654c9.b93774","type":"ui_group","name":"TextInput","tab":"b4480307.eea488","order":1,"disp":true,"width":"6","collapse":false},{"id":"f907f557.afb4e8","type":"ui_tab","name":"IP","icon":"dashboard","disabled":false,"hidden":false},{"id":"23ae81f3.a18136","type":"ui_group","name":"IPInput","tab":"f907f557.afb4e8","order":1,"disp":true,"width":"6","collapse":false},{"id":"4cd0ed56.445ecc","type":"function","z":"d3a188d4.8d7a5","name":"","func":"var val = msg.payload;\nnode.warn(\"val = \" + val);\nvar res;\nvar numval =\"\";\nvar hexval;\nvar endval;\nvar Hex = new Array(16);\nHex[0] = 0;\nHex[1] = 1;\nHex[2] = 2;\nHex[3] = 3;\nHex[4] = 4;\nHex[5] = 5;\nHex[6] = 6;\nHex[7] = 7;\nHex[8] = 8;\nHex[9] = 9;\nHex[10] = \"A\";\nHex[11] = \"B\";\nHex[12] = \"C\";\nHex[13] = \"D\";\nHex[14] = \"E\";\nHex[15] = \"F\";\nwhile(res > 0)\n{\n    res = val%16;\n    node.warn(\"res = \" + res);\n    for(i=0;i<10;i+1)\n    {\n        if(res == Hex[i])\n        {\n            numval = res + numval;\n            node.warn(\"numval = \"+ numval);\n        }\n        \n        else if (res<9>16)\n        {\n            hexval = Hex[res];\n            node.warn(\"hexval = \" +hexval);\n       }\n   }\n    val = val/16;\n    \n}\n          endval = numval+hexval;\n          msg.payload = {payload:res};\n           \nreturn msg;\n\n\n","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started","finalize":"","libs":[],"x":320,"y":140,"wires":[["539cd750.5ac72"]]},{"id":"539cd750.5ac72","type":"debug","z":"d3a188d4.8d7a5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":550,"y":140,"wires":[]},{"id":"203fa43.a0659dc","type":"inject","z":"62f0c6d6.6621a8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"123","payloadType":"str","x":250,"y":160,"wires":[["183469fc.d0de16"]]},{"id":"32f2b028.6ea86","type":"debug","z":"62f0c6d6.6621a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":140,"wires":[]},{"id":"183469fc.d0de16","type":"function","z":"62f0c6d6.6621a8","name":"","func":"msg.payload = toHex(msg.payload)\nreturn msg;\n\nfunction toHex(d) {\n    return  (\"0\"+(Number(d).toString(16))).slice(-2).toUpperCase()\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":160,"wires":[["32f2b028.6ea86"]]},{"id":"fee7c10.0b900c","type":"function","z":"62f0c6d6.6621a8","name":"","func":"msg.payload = msg.payload.toString(16);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":240,"wires":[["32f2b028.6ea86"]]},{"id":"d9f220b3.94f568","type":"inject","z":"62f0c6d6.6621a8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"123","payloadType":"num","x":250,"y":240,"wires":[["fee7c10.0b900c"]]},{"id":"1e9af25a.aa946e","type":"inject","z":"d3a188d4.8d7a5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"201","payloadType":"num","x":80,"y":140,"wires":[["4cd0ed56.445ecc"]]},{"id":"ff27fc3f.8b624","type":"function","z":"d3a188d4.8d7a5","name":"","func":"\nnode.warn(\"val = \" + val);\nvar res;\nvar numval;\nvar hexval;\nvar endval;\nvar Hex = new Array(16);\nHex[0] = 0;\nHex[1] = 1;\nHex[2] = 2;\nHex[3] = 3;\nHex[4] = 4;\nHex[5] = 5;\nHex[6] = 6;\nHex[7] = 7;\nHex[8] = 8;\nHex[9] = 9;\nHex[10] = \"A\";\nHex[11] = \"B\";\nHex[12] = \"C\";\nHex[13] = \"D\";\nHex[14] = \"E\";\nHex[15] = \"F\";\nvar val = msg.payload;\nvar res1 = val%16;\nwhile(res>0)\nnode.warn(\"awd = \" + res1);\n{\n    res = val%16;\n    numval = Hex[res];\n    endval = endval+\"\"+numval;\n}\nnode.warn(\"numval = \" + numval);\nmsg.payload = {payload:endval};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":420,"wires":[[]]}]

this is the code iv currently got. The input to the function node is a inject node.

This code ↑ works. What is your issue?

Could you post your code that is not working (with a suitable inject to simulate the problem)

wait what? how does it work for you its not working at all for me, my payload returns as undefined and the only node.warn that works is the one outside of the while statement. im confused as to how its working for you?

The flow you posted above in your last message...

is this...

q3wriAJKcO

Did you post the wrong flow?

ohhhh yeah that's the example @edje11 posted in the first reply apologies. there should be another flow but ill repost my flow

[{"id":"d3a188d4.8d7a5","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"4cd0ed56.445ecc","type":"function","z":"d3a188d4.8d7a5","name":"","func":"var val = msg.payload;\nnode.warn(\"val = \" + val);\nvar res;\nvar numval =\"\";\nvar hexval;\nvar endval;\nvar Hex = new Array(16);\nHex[0] = 0;\nHex[1] = 1;\nHex[2] = 2;\nHex[3] = 3;\nHex[4] = 4;\nHex[5] = 5;\nHex[6] = 6;\nHex[7] = 7;\nHex[8] = 8;\nHex[9] = 9;\nHex[10] = \"A\";\nHex[11] = \"B\";\nHex[12] = \"C\";\nHex[13] = \"D\";\nHex[14] = \"E\";\nHex[15] = \"F\";\nwhile(res > 0)\n{\n    res = val%16;\n    node.warn(\"res = \" + res);\n    for(i=0;i<10;i+1)\n    {\n        if(res == Hex[i])\n        {\n            numval = res + numval;\n            node.warn(\"numval = \"+ numval);\n        }\n        \n        else if (res<9>16)\n        {\n            hexval = Hex[res];\n            node.warn(\"hexval = \" +hexval);\n       }\n   }\n    val = val/16;\n    \n}\n          endval = numval+hexval;\n          msg.payload = {payload:res};\n           \nreturn msg;\n\n\n","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started","finalize":"","libs":[],"x":320,"y":140,"wires":[["539cd750.5ac72"]]},{"id":"539cd750.5ac72","type":"debug","z":"d3a188d4.8d7a5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":140,"wires":[]},{"id":"1e9af25a.aa946e","type":"inject","z":"d3a188d4.8d7a5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"201","payloadType":"num","x":80,"y":140,"wires":[["4cd0ed56.445ecc"]]},{"id":"ff27fc3f.8b624","type":"function","z":"d3a188d4.8d7a5","name":"","func":"\nnode.warn(\"val = \" + val);\nvar res;\nvar numval;\nvar hexval;\nvar endval;\nvar Hex = new Array(16);\nHex[0] = 0;\nHex[1] = 1;\nHex[2] = 2;\nHex[3] = 3;\nHex[4] = 4;\nHex[5] = 5;\nHex[6] = 6;\nHex[7] = 7;\nHex[8] = 8;\nHex[9] = 9;\nHex[10] = \"A\";\nHex[11] = \"B\";\nHex[12] = \"C\";\nHex[13] = \"D\";\nHex[14] = \"E\";\nHex[15] = \"F\";\nvar val = msg.payload;\nvar res1 = val%16;\nwhile(res>0)\nnode.warn(\"awd = \" + res1);\n{\n    res = val%16;\n    numval = Hex[res];\n    endval = endval+\"\"+numval;\n}\nnode.warn(\"numval = \" + numval);\nmsg.payload = {payload:endval};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":420,"wires":[[]]}]

This should be my Flow apologies. I just woke up still booting :smiley:

@Apple what are you actually trying to achieve here?

What value do you expect input of 201 to be output as?

EDIT..

Sorry, your first post explains it "I'm trying to convert Decimal to Hex with raw code"

When you say "raw code" does that not include number.toString("16") like in the simple demo @edje11 posted? or must you extract each character and do the conversion without any calls to the likes of toString

else if (res<9>16)
That isn't valid javascript. Well actually it is valid, but it tests res<9 which gives true or false and then says is that >16, which isn't very useful. I assume that what you mean is
else if (res > 9 && res < 16)
though quite how you thought your original would do that I don't know.

Yes i need to do the conversion myself without any calls of toString

ah I see ok. Apologies iv only recently started with Java and node red I'm about 3 weeks in now on my 3rd "challenge" so learning as I go along

It is javascript, not java (which is completely different). Whoever decided to call it javascript has a lot to answer for.