# Hex String to Binary

**URL:** <https://discourse.nodered.org/t/hex-string-to-binary/22073>\
**Category:** General\
**Created:** [21 February 2020 21:36 UTC](https://discourse.nodered.org/t/hex-string-to-binary/22073 "2020-02-21T21:36:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheadehAnank](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@TheadehAnank](https://discourse.nodered.org/u/TheadehAnank)\
**Post date:** [21 February 2020 21:36 UTC](https://discourse.nodered.org/t/hex-string-to-binary/22073/1 "2020-02-21T21:36:44Z")

</div>

Hello - I am trying to convert a Hex String to Binary, then sending binary to a device using TCP Request Node. However, the device is not responding correctly. So far, I have the following code. Not really sure what I'm doing. The hex to binary string seems to be working, but I'm not sure about how to convert the binary string to raw binary. Any advice would be appreciated.

Inject string: "57 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CF 1B 55 AA 00 55 AA 02 00 00 00 00 00 00 55 AA 40 00 00 00 00 01 54 00 00 02 00 00 00 00 00"

```auto
Function fnHex2Bin
 	msg.payload = msg.payload.split(" ");
 	var result = "";
 	msg.payload.forEach(str => {
 	 result += (parseInt(str,16).toString(2)); //hex base-16
 	})
 	msg.payload = new Buffer(result);
 return msg

```

---

<div class="post-metadata">

**Author:** ![dathor](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@dathor](https://discourse.nodered.org/u/dathor)\
**Post date:** [22 February 2020 11:40 UTC](https://discourse.nodered.org/t/hex-string-to-binary/22073/2 "2020-02-22T11:40:03Z")

</div>

what about this...

```auto
Function fnHex2Bin
 	msg.payload = msg.payload.split(" ");
 	var result = [];
 	msg.payload.forEach(str => {
 	 result.push(parseInt(str,16)); //hex base-16
 	})
 	msg.payload = new Buffer(result);
 return msg

```

---

<div class="post-metadata">

**Author:** ![TheadehAnank](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@TheadehAnank](https://discourse.nodered.org/u/TheadehAnank)\
**Post date:** [22 February 2020 15:24 UTC](https://discourse.nodered.org/t/hex-string-to-binary/22073/3 "2020-02-22T15:24:15Z")

</div>

Yes! That Worked! I've been trying to sort out this integration for weeks now. Thank you!

---

<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:** [22 April 2020 15:24 UTC](https://discourse.nodered.org/t/hex-string-to-binary/22073/4 "2020-04-22T15:24:26Z")

</div>

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