# Need help on reverse hex

**URL:** <https://discourse.nodered.org/t/need-help-on-reverse-hex/48445>\
**Category:** General\
**Created:** [15 July 2021 08:40 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445 "2021-07-15T08:40:13Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![darw82](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@darw82](https://discourse.nodered.org/u/darw82)\
**Post date:** [15 July 2021 08:40 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/1 "2021-07-15T08:40:13Z")

</div>

Hello,  
I'm newbie on Node-Red, I need help on reversing hex output from buffer parser.  
Please let me know how to do this properly.

Thank you.

 ![convert](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6b4ac84fef3d311ede0a14d63b7e5a32dd4b75a4.jpeg)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 July 2021 09:08 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/2 "2021-07-15T09:08:46Z")

</div>

That is an unusual arrangement. No amount of byte-swapping or BE/LE operations will generate that hex string.

You will have to either...

- pre-process the data array to reverse the 4 nibbles of each 16bit integer before sending to buffer parser  
_OR_
- post-process the HEX string after buffer parser and reverse each 4 characters.

  

Are you 100% certain that is what you need?

- Where does the source data come from and what does it represent?
- How is the final data used?

---

<div class="post-metadata">

**Author:** ![darw82](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@darw82](https://discourse.nodered.org/u/darw82)\
**Post date:** [15 July 2021 09:20 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/3 "2021-07-15T09:20:28Z")

</div>

Hi Steve,  
Thanks for your reply. I have no idea, why would the manufacturer of product make it so complicated just to read the Serial Number. This is the data read from Modbus

 ![convert1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7cdabff3b12582c78032ed1e0b5d40cdcb794e41.jpeg)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 July 2021 12:25 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/4 "2021-07-15T12:25:16Z")

</div>

Here is a version with a pre-processor function "Reverse Digits"

![kk8vhvzcrM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/4853684e679be94cc56df1a9d4bbab31289cdcf0.gif)

```auto
[{"id":"48710450b1015ae2","type":"buffer-parser","z":"f9687f620c582b41","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"hex","name":"hex","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":1290,"y":1980,"wires":[["be3855e7e3db7224"]]},{"id":"c01496387b913dbd","type":"inject","z":"f9687f620c582b41","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":1175,"y":1860,"wires":[["93a9fe50e7dce0f6"]],"l":false},{"id":"93a9fe50e7dce0f6","type":"function","z":"f9687f620c582b41","name":"[0xe011, 0x9111, 0x32a, 0x100]","func":"msg.payload = [0xe011, 0x9111, 0x32a, 0x100]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1350,"y":1860,"wires":[["12016d8462cdf276"]]},{"id":"be3855e7e3db7224","type":"debug","z":"f9687f620c582b41","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1450,"y":1980,"wires":[]},{"id":"12016d8462cdf276","type":"function","z":"f9687f620c582b41","name":"Reverse digits in 16bit data array","func":"let data = msg.payload;\n\nmsg.payload = data.map(e => {\n let n1 = (e & 0xf000) >> 12\n let n2 = (e & 0x0f00) >> 4\n let n3 = (e & 0x00f0) << 4\n let n4 = (e & 0x000f) << 12\n return n1 | n2 | n3 | n4;\n})\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1360,"y":1920,"wires":[["48710450b1015ae2"]]},{"id":"57f54b59b6350372","type":"inject","z":"f9687f620c582b41","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":1175,"y":1820,"wires":[["f0b8995005a4403e"]],"l":false},{"id":"f0b8995005a4403e","type":"function","z":"f9687f620c582b41","name":"[0x4321, 0x8765, 0xcba9, 0x0fed]","func":"msg.payload = [0x4321, 0x8765, 0xcba9, 0x0fed]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1360,"y":1820,"wires":[["12016d8462cdf276"]]}]

```

---

<div class="post-metadata">

**Author:** ![darw82](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@darw82](https://discourse.nodered.org/u/darw82)\
**Post date:** [15 July 2021 15:00 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/5 "2021-07-15T15:00:23Z")

</div>

WOW... It's works. Thank you very much for the help. Only left small problem now.  
There seems inconsistency with the product datasheet. I will check this with the manufacturer.

Thanks again for your help. 😉

 ![Screenshot_5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1ad15a599836a8b1b8a085616d949c71904c1f46.jpeg)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 July 2021 15:21 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/6 "2021-07-15T15:21:53Z")

</div>

If you look closely at the example shown in the datasheet you will see it is even more complex than at first appears. The most significant hex digit of the second word is not used (8 does not appear appear in the result).

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 July 2021 15:33 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/7 "2021-07-15T15:33:25Z")

</div>

Though perhaps that is a misprint, as if it were correct the algorithm would be even worse at getting the correct number I think. Is the output you showed in the first post exactly as received from modbus?

---

<div class="post-metadata">

**Author:** ![darw82](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@darw82](https://discourse.nodered.org/u/darw82)\
**Post date:** [15 July 2021 15:45 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/8 "2021-07-15T15:45:34Z")

</div>

hahah.. yeah, I didn't expect the solution was that complicated just to read serial number value. I will try to read directly on the device by tomorrow morning. Currently the device is offline, since it's Grid Tie Inverter.

---

<div class="post-metadata">

**Author:** ![darw82](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@darw82](https://discourse.nodered.org/u/darw82)\
**Post date:** [16 July 2021 02:30 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/9 "2021-07-16T02:30:11Z")

</div>

Here is actual output from Modbus. I've tried make modification on solution provided by Steve, to discard last digit on the string output. But cannot make it right, it's really hard for me to understand the code.

 ![S1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/ab353fd55cc18c07070782f0b4fe6205dee8af37.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 July 2021 06:11 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/10 "2021-07-16T06:11:58Z")

</div>

My guess is the serial number is simply 15 characters.

Use the original function code I wrote & simply remove the last character of the hex generated using a function of JSONata to trim the string to 15 chars

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 July 2021 06:51 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/11 "2021-07-16T06:51:11Z")

</div>

Select that flow (CTRL+click the timestamp inject node) then export (CTRL+E) to a reply & I'll show you

---

<div class="post-metadata">

**Author:** ![darw82](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@darw82](https://discourse.nodered.org/u/darw82)\
**Post date:** [16 July 2021 07:37 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/12 "2021-07-16T07:37:26Z")

</div>

Here is the code. Many thanks for your help. 🙂

[{"id":"324c152.7ff62ea","type":"inject","z":"769a8e99.43d96","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":"3","x":2560,"y":140,"wires":[["379a0886.13d658"]]}]

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 July 2021 07:40 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/13 "2021-07-16T07:40:51Z")

</div>

That is only the inject.

> [@Steve-Mcl](#):
>
> Select that flow (CTRL+click the timestamp inject node)

Also, be sure to wrap flow in a code block to prevent forum damaging the code

`````  
`[paste: flow, code: here]`  
`````

### EDIT

**Hold SHIFT + CLICK (to select all connected nodes) then CTRL+E export**

![IEM2pYO6zf](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/e/becf812cbc117df3ed92a6a0a3b012a09adbddac.gif)

---

<div class="post-metadata">

**Author:** ![darw82](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@darw82](https://discourse.nodered.org/u/darw82)\
**Post date:** [16 July 2021 07:51 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/14 "2021-07-16T07:51:20Z")

</div>

> [@Steve-Mcl](#):
>
> Also, be sure to wrap flow in a code block to prevent forum damaging the code

ahh.. just realize, I can select all connected node with SHIFT + Click..

[{"id":"324c152.7ff62ea","type":"inject","z":"769a8e99.43d96","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":"3","x":2560,"y":140,"wires":[["379a0886.13d658"]]},{"id":"379a0886.13d658","type":"function","z":"769a8e99.43d96","name":"Modbus Simulator","func":"var p0 = 57361;\nvar p1 = 37137;\nvar p2 = 810;\nvar p3 = 256;\n\n\nmsg.payload = { p0, p1, p2, p3 };\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":2770,"y":140,"wires":[["442fd7a8.a2f868","482de54b.9273ec"]]},{"id":"442fd7a8.a2f868","type":"debug","z":"769a8e99.43d96","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":3010,"y":120,"wires":},{"id":"482de54b.9273ec","type":"function","z":"769a8e99.43d96","name":"[0xe011, 0x9111, 0x32a, 0x100]","func":"msg.payload = [msg.payload.p0, msg.payload.p1, msg.payload.p2, msg.payload.p3]\nreturn msg;","outputs":1,"noerr":0,"x":2750,"y":220,"wires":[["d73ae348.0d79b"]]},{"id":"d73ae348.0d79b","type":"function","z":"769a8e99.43d96","name":"Reverse digits in 16bit data array","func":"let data = msg.payload;\n\nmsg.payload = data.map(e =\> {\n let n1 = (e & 0xf000) \>\> 12\n let n2 = (e & 0x0f00) \>\> 4\n let n3 = (e & 0x00f0) \<\< 4\n let n4 = (e & 0x00f) \<\< 12\n return n1 | n2 | n3 | n4;\n})\n\nreturn msg;","outputs":1,"noerr":0,"x":2760,"y":260,"wires":[["d1a5a539.599b78"]]},{"id":"d1a5a539.599b78","type":"buffer-parser","z":"769a8e99.43d96","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"hex","name":"hex","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":2770,"y":300,"wires":[["b64a60b0.be0e1"]]},{"id":"b64a60b0.be0e1","type":"debug","z":"769a8e99.43d96","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":3050,"y":360,"wires":}]

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 July 2021 07:53 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/15 "2021-07-16T07:53:25Z")

</div>

You code is broken and cannot be imported as you did not wrap it in a code block.

In order to make code more readable and importable it is important to surround your code with three backticks  
`````  
`like this`  
`````

You can edit and correct your post by clicking the pencil icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 July 2021 08:06 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/16 "2021-07-16T08:06:18Z")

</div>

Please in future use a code block when posting code or flows.

So here is all it needed...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/5/a59df1110479868094bf838ad7aad13ba80a653a.png)

The **"trim serial no"** function code..

```javascript
msg.payload = msg.payload.substr(0, 15).toUpperCase(); //trim to 15 chars & set upper case
return msg;

```

---

<div class="post-metadata">

**Author:** ![darw82](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@darw82](https://discourse.nodered.org/u/darw82)\
**Post date:** [16 July 2021 09:14 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/17 "2021-07-16T09:14:38Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> msg.payload = msg.payload.substr(0, 15).toUpperCase(); //trim to 15 chars & set upper case
> return msg;
> 
> ```

Works perfect. Thank you for your help, I appreciate it,.. for all the time you spent on this

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 September 2021 09:15 UTC](https://discourse.nodered.org/t/need-help-on-reverse-hex/48445/18 "2021-09-14T09:15:23Z")

</div>

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