title: JavaScript: Need functions to convert a string containing binary to hex, then convert back to binary
body: <p>Lets say I have a string in JavaScript with binary data in it. It may look like this:</p>
  
  <pre><code>var binary = '00001000010001000101010100001110';
  </code></pre>
  
  <p>I need some reliable functions to convert this into a hexadecimal string, and then convert back from that hexadecimal to a binary string again. I know about the following functions</p>
  
  <pre><code>// Convert binary to hexadecimal
  var hex = parseInt(binaryCharacters, 2).toString(16);
  
  // Convert hexadecimal to binary
  var binary = parseInt(hex, 16).toString(2)
  </code></pre>
  
  <p>But I'm not sure how to convert the whole string at once. Am I right in understanding I need to convert each 4 binary bits at a time into a single hexadecimal character? Then to get back to binary I loop through each hexadecimal character and convert to binary again?</p>
  
  <p>I have hunted for some simple examples doing this in JavaScript but can't find any.</p>
  
  <p>Many thanks</p>