# Help with converting string to hex array

**URL:** <https://discourse.nodered.org/t/help-with-converting-string-to-hex-array/65091>\
**Category:** General\
**Created:** [14 July 2022 11:30 UTC](https://discourse.nodered.org/t/help-with-converting-string-to-hex-array/65091 "2022-07-14T11:30:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PAT](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@PAT](https://discourse.nodered.org/u/PAT)\
**Post date:** [14 July 2022 11:30 UTC](https://discourse.nodered.org/t/help-with-converting-string-to-hex-array/65091/1 "2022-07-14T11:30:22Z")

</div>

Hi all please can someone help me with a conversion problem

my incomming data from serial port looks like this.  
 ![Screenshot 2022-07-14 131314](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/a/bac41f96b23df769b8aba510fc57443895de6b58.png)  
i have been able to break it up into the correct values and place in an array which im happy with  
 ![Screenshot 2022-07-14 131359](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/ab8e1beccb61cb747dd8aec4362c598d8404dbaf.png)  
however node red cannot convert the hex value to a readable number as the format is not correct  
"0DD2" when it should be "0x0DD2" telling the system that it is a 16bit hex number.

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [14 July 2022 11:37 UTC](https://discourse.nodered.org/t/help-with-converting-string-to-hex-array/65091/2 "2022-07-14T11:37:07Z")

</div>

`msg.payload=parseInt("0DD2", 16);` should work

Depending how you slice the string now, the above can be done during slicing.

---

<div class="post-metadata">

**Author:** ![PAT](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@PAT](https://discourse.nodered.org/u/PAT)\
**Post date:** [14 July 2022 11:54 UTC](https://discourse.nodered.org/t/help-with-converting-string-to-hex-array/65091/3 "2022-07-14T11:54:21Z")

</div>

> [@edje11](#):
>
> msg.payload=parseInt("0DD2", 16);

that worked partially . but it has replaced all the data with 0dd2. defanitly a step in the right direction  
 ![Screenshot 2022-07-14 135218](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48306b95438689e66935cbfd5c336ac92afe1939.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:** [14 July 2022 12:03 UTC](https://discourse.nodered.org/t/help-with-converting-string-to-hex-array/65091/4 "2022-07-14T12:03:42Z")

</div>

If you have node-red-contrib-buffer parser, you can do pretty much anything you need. I imagine you actually want real numbers (not hex strings - but you can have thos too) e.g...

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1e371fc8be0e3078c0f0da363382066dd42c2d37.png)

flow...

```auto
[{"id":"a4466e01f63dcff8","type":"inject","z":"8fb7704d922a7884","name":"03051234eeffabcd32109999","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"03051234eeffabcd32109999","payloadType":"str","x":280,"y":680,"wires":[["bd0bcb99df2b12f3"]]},{"id":"6cdb292d19b218ca","type":"debug","z":"8fb7704d922a7884","name":"debug 47","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":680,"wires":[]},{"id":"bd0bcb99df2b12f3","type":"buffer-parser","z":"8fb7704d922a7884","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"buffer","name":"asBuffer","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""},{"type":"int16be","name":"int16be","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16le","name":"uint16be","offset":0,"length":-1,"offsetbit":0,"scale":"1","mask":""},{"type":"hex","name":"str0","offset":0,"length":2,"offsetbit":0,"scale":"1","mask":""},{"type":"hex","name":"str1","offset":2,"length":2,"offsetbit":0,"scale":"1","mask":""},{"type":"hex","name":"str2","offset":4,"length":2,"offsetbit":0,"scale":"1","mask":""},{"type":"hex","name":"str3","offset":6,"length":2,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":510,"y":680,"wires":[["6cdb292d19b218ca"]]}]

```

---

<div class="post-metadata">

**Author:** ![PAT](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@PAT](https://discourse.nodered.org/u/PAT)\
**Post date:** [14 July 2022 12:14 UTC](https://discourse.nodered.org/t/help-with-converting-string-to-hex-array/65091/5 "2022-07-14T12:14:28Z")

</div>

hi guys just got it working with a small change to edje11 code now it works well . i have been fighting with this number for 4 days now .

msg.payload=parseInt(msg.payload, 16); this works between the split and join nodes.

Btw i love your buffer passer node i use it for everything however when i pass this number through it i get  
 ![Screenshot 2022-07-14 140703](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b1e0d45cd51e35c2e7de9cd60bf38e4d3621b1c4.png)

with that function node it comes out like this  
 ![Screenshot 2022-07-14 140757](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/1/f194c824d751221dc38e9b575f426e26f7aba8a2.png) which is correct for what im trying to do then off to the buffer parser node to name everything and off to influx.

thanks for the help much app

---

<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:** [14 July 2022 12:21 UTC](https://discourse.nodered.org/t/help-with-converting-string-to-hex-array/65091/6 "2022-07-14T12:21:16Z")

</div>

> [@PAT](#):
>
> Btw i love your buffer passer node i use it for everything however when i pass this number through it i get

could you copy the data that was going into the buffer parser (add a debug node & use the `Copy Value` button on the debug message in the debug sidebar to grab the value)

---

<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:** [12 September 2022 12:21 UTC](https://discourse.nodered.org/t/help-with-converting-string-to-hex-array/65091/7 "2022-09-12T12:21:17Z")

</div>

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