Hex to Binary Array

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


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

Also, the Workflow Lab has some great examples too.

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"

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:

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
      .padStart(8, "0");
  return binaryString;

payload.working.binary = bufferToBinaryString(data);

The Buffer object 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 npm module.

That worked, thank you