title: base64 encoding: input character
body: <p>I'm trying to understand what the input requirements are for base64 encoding. Nicholas Zakas, who I have tremendous respect for has an article here where he quotes a specification that an error should be thrown if input contains any character with a code higher than 255 <a href="http://www.nczonline.net/blog/2009/12/08/computer-science-in-javascript-base64-encoding/" rel="nofollow noreferrer">Zakas Article on base64</a></p>
  <blockquote>
  <p>Before even attempting to base64 encode a string, you should check to see if the string contains only ASCII characters. Since base64 encoding requires eight bits per input character, any character with a code higher than 255 cannot be accurately represented. The specification indicates that an error should be thrown in this case:</p>
  </blockquote>
  <pre><code>if (/([^\u0000-\u00ff])/.test(text)){
          throw new Error(&quot;Can't base64 encode non-ASCII characters.&quot;);
      }
  </code></pre>
  <p>He provides a link in another separate part of the article to the <a href="https://www.rfc-editor.org/rfc/rfc3548" rel="nofollow noreferrer">RFC 3548</a> but I don't see any input requirements other than:</p>
  <blockquote>
  <p>Implementations MUST reject the encoding if it contains characters
  outside the base alphabet when interpreting base encoded data, unless
  the specification referring to this document explicitly states
  otherwise.</p>
  </blockquote>
  <p>Not sure what &quot;base alphabet&quot; means but perhaps this is what Zakas is referring to. But by saying they must reject the encoding it seems to imply that this is something that has already been encoded as opposed to the input (of course if the input is invalid it will also show up in the encoding so perhaps the point is moot).</p>
  <p>A bit confused on what the standard is.</p>