title: Converting between base-2 (binary), base-16 (hexadecimal), and base-64 in Javascript
body: <p>Converting ASCII strings to and from hex and base-64 is fairly simple in Javascript. However, dealing with binary and UTF-encoded strings throws a wrench into this.</p>
  
  <p>Javascript's built-in atob() and btoa() functions do not work with UTF-encoded strings, which is a major problem for strings coming from elements (e.g. inputs) in an HTML document that declares a UTF-8 charset. Additionally, it seems that base-64 can only be directly encoded using strings that are already in ASCII-encoded hex, with no direct way provided to convert a binary string (either ASCII or UTF-8-encoded) to base-64.</p>
  
  <p>To compound the issue further, it appears that nearly all of the questions posted on SO and elsewhere assume that "binary string" is equivalent to binary data represented in a hex-encoded string, as opposed to a string consisting of base-2 numbers.</p>
  
  <p>Given a UTF-8 or ASCII-encoded string consisting of binary, hex, or base-64 characters, how would you convert between the three?</p>