# Hex to Binary Array

**URL:** <https://forums.losant.com/t/hex-to-binary-array/2861>\
**Category:** Help\
**Tags:** workflow\
**Created:** [April 24, 2020, 1:40pm UTC](https://forums.losant.com/t/hex-to-binary-array/2861 "2020-04-24T13:40:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stewart\_McCallum](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/stewart_mccallum/32/2490_2.png) [@Stewart\_McCallum](https://forums.losant.com/u/Stewart_McCallum)\
**Post date:** [April 24, 2020, 1:40pm UTC](https://forums.losant.com/t/hex-to-binary-array/2861/1 "2020-04-24T13:40:58Z")

</div>

Pretty sure I need to do this in a function node, but I have a hex string “31489E88AED06C3333”, for example. Byte [0] is a status byte in binary “110001”.  
I need to convert that HEX (31) to a BIN array , as every bit has a purpose.  
Can anyone point me to an example ?

Thanks in advance  
S

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [April 24, 2020, 2:22pm UTC](https://forums.losant.com/t/hex-to-binary-array/2861/3 "2020-04-24T14:22:20Z")

</div>

@Stewart_McCallum,

Hi! Welcome to the Losant Forums. You can find a good example of that in this post:

> [@\[Solved\] Hex to 32-bit float](https://forums.losant.com/t/solved-hex-to-32-bit-float/1339):
>
> how to convert hex (in string) to 32-bit float (IEEE 754)?

Also, the [Workflow Lab](https://docs.losant.com/workflows/workflow-lab/lab/) has some great examples too.

---

<div class="post-metadata">

**Author:** ![Stewart\_McCallum](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/stewart_mccallum/32/2490_2.png) [@Stewart\_McCallum](https://forums.losant.com/u/Stewart_McCallum)\
**Post date:** [April 24, 2020, 2:48pm UTC](https://forums.losant.com/t/hex-to-binary-array/2861/5 "2020-04-24T14:48:15Z")

</div>

Thanks Fox, perhaps I missed something ?  
A; I’ve seen that post before but don’t understand how to get a binary array. Is the another version of  
" b.readFloatLE(0);" that outputs binary ?  
B: if I copy and that example and run just for S\*\*ts and Giggles, I an errror" **FunctionNodeError**  
The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined"

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [April 24, 2020, 5:34pm UTC](https://forums.losant.com/t/hex-to-binary-array/2861/9 "2020-04-24T17:34:02Z")

</div>

Hey Stewart,

Assuming that your hex string is on the payload at `working.hex`, here’s a Function Node that will result in a string of ones and zeros:

```auto
let data = Buffer.from(payload.working.hex, 'hex');

let bufferToBinaryString = (buf) => {
  let binaryString = "";
  for (let offset = 0, length = buf.length; offset < length; offset++) {
    binaryString += buf
      .readUInt8(offset)
      .toString(2)
      .padStart(8, "0");
  }
  return binaryString;
}

payload.working.binary = bufferToBinaryString(data);

```

 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/4/4bb1d851e6dc184dd64f66cccd71771bb99af548.png)

The [Buffer object](https://nodejs.org/api/buffer.html) has lots of operations to work with binary data, but coverting it to a string of ones and zeros is unfortunately not one of them. The `bufferToBinaryString` function above was pulled from the [BitString](https://github.com/crsgit/node-bitstring) npm module.

---

<div class="post-metadata">

**Author:** ![Stewart\_McCallum](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/stewart_mccallum/32/2490_2.png) [@Stewart\_McCallum](https://forums.losant.com/u/Stewart_McCallum)\
**Post date:** [May 6, 2020, 3:00pm UTC](https://forums.losant.com/t/hex-to-binary-array/2861/12 "2020-05-06T15:00:08Z")

</div>

That worked, thank you
