Encoding and decoding base64 using an ArrayBuffer
If you're going to use ArrayBuffer and DataView for your binary data and carry binary data as base64 strings, you can use the Mozilla-written functions at https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_.232_.E2.80.93_rewriting_atob%28%29_and_btoa%28%29_using_TypedArrays_and_UTF-8 to do so. They provide the functions strToUTF8Arr and UTF8ArrToStr to perform UTF-8 encoding and decoding, as well as base64EncArr and base64DecToArr to convert between base64 strings and array buffers.
How to do it…
Here's an interconversion example that encodes a text string as UTF-8, then converts the text into base64, then shows the base64 results, and finally converts the base64 to ArrayBuffer of UTF-8 data before converting the UTF-8 back to a regular character string:
var input = "Base 64 example"; var inputAsUTF8 = strToUTF8Arr(input); var base64 = base64EncArr(inputAsUTF8); alert(base64); var outputAsUTF8...