# Help with creating a buffer of hex bytes from a string of hex words

**URL:** <https://discourse.nodered.org/t/help-with-creating-a-buffer-of-hex-bytes-from-a-string-of-hex-words/29259>\
**Category:** General\
**Created:** [30 June 2020 19:30 UTC](https://discourse.nodered.org/t/help-with-creating-a-buffer-of-hex-bytes-from-a-string-of-hex-words/29259 "2020-06-30T19:30:27Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [30 June 2020 21:14 UTC](https://discourse.nodered.org/t/help-with-creating-a-buffer-of-hex-bytes-from-a-string-of-hex-words/29259/8 "2020-06-30T21:14:17Z")

</div>

I knew you'd crack this a lot faster. Meanwhile I was only able to cobble together this inferior version:

```auto
let str = msg.payload.split(' ').join('');
msg.payload = hexStringToBuffer(str);

return msg;

function hexStringToBuffer(str) {
  let a = [];
  for (let i = 0, len = str.length; i < len; i += 2) {
    a.push(parseInt(str.substr(i,2), 16));
  }
  
  return Buffer.from(a);
}

```

Great to learn there's even a simpler version. But what boggled me the most is why `str.replaceAll(' ', '');` gave me an unknown function error. I'm running Node 14 and I'm pretty sure I've used the syntax multiple times earlier on. With that said, my JS seems to be getting rusty after a 6 month of break writing it at work...

---

_[View the full topic](https://discourse.nodered.org/t/help-with-creating-a-buffer-of-hex-bytes-from-a-string-of-hex-words/29259)._
