W3C home > Mailing lists > Public > whatwg@whatwg.org > August 2013

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

From: Simon Pieters <simonp@opera.com>
Date: Mon, 05 Aug 2013 21:41:26 +0200
To: whatwg@whatwg.org, "Chang Shu" <cshu01@gmail.com>
Message-ID: <op.w1czncwaidj3kv@simons-macbook-pro.local>
On Mon, 05 Aug 2013 16:10:50 +0200, Chang Shu <cshu01@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
Received on Monday, 5 August 2013 19:36:57 UTC

This archive was generated by hypermail 2.3.1 : Monday, 13 April 2015 23:09:23 UTC