[whatwg] BinaryEncoding for Typed Arrays using window.btoa and window.atob

Simon Pieters simonp at opera.com
Mon Aug 5 12:41:26 PDT 2013


On Mon, 05 Aug 2013 16:10:50 +0200, Chang Shu <cshu01 at gmail.com> wrote:

> window.btoa
>
> Summary
>
> Creates a base-64 encoded ASCII string from either a "string" of
> binary data or a Typed Array.
>
> Syntax
>
> var encodedData = window.btoa(dataToEncode);
>
> Note that there is no syntax change in window.btoa API.
>
> Example
>
> var encodedData = window.btoa("hello"); //encode a string. Consider
> the string as 'binary'
>
> var arr = new Int32Array(3);
> arr[0] = 1;
> arr[1] = 2;
> arr[2] = 3;
> var encodedData = window.btoa(arr); //encode integer data into a base-64  
> string
>
> window.atob
>
> Summary
>
> Decodes a base-64 encoded ASCII string into a "string" of binary data
> and a Typed Array if parameter provided.
>
> Syntax
>
> var decodedArr = new Int32Array();
> var decodedData = window.atob(encodedData, [Optinoal] decodedArr);
>
> Note that the 2nd parameter is optional which keeps the backward  
> compatibility.
>
> Example
>
> var arr = new Int32Array(3);
> arr[0] = 1;
> arr[1] = 2;
> arr[2] = 3;
> var encodedData = window.btoa(arr); //encode integer data into a base-64  
> string
>
> var newarr = new Int32Array();
> window.atob(encodedData, newarr); //decode base-64 string back to  
> integer array
> //newarr[0] should be 1, newarr[1] should be 2 and newarr[2] should be 3.

Is there a reason to support an arbitrary typed array for atob rather than  
returning a new typed array?

e.g.

var newarr = atob(encodedData, {typedarray:true});

(I'm not sure which view is most appropriate to return.)

-- 
Simon Pieters
Opera Software



More information about the whatwg mailing list