W3C home > Mailing lists > Public > public-webapps@w3.org > January to March 2012

Re: String to ArrayBuffer

From: James Robinson <jamesr@google.com>
Date: Wed, 11 Jan 2012 14:49:45 -0800
Message-ID: <CAD73mdLX0UYJC7V_i=V2fg6PmvS2cO+j7W2uVeMR2B8_ukveFw@mail.gmail.com>
To: Charles Pritchard <chuck@jumis.com>
Cc: Webapps WG <public-webapps@w3.org>
On Wed, Jan 11, 2012 at 2:45 PM, Charles Pritchard <chuck@jumis.com> wrote:

> Currently, we can asynchronously use BlobBuilder with FileReader to get an
> array buffer from a string.
> We can of course, use code to convert String.fromCharCode into a
> Uint8Array, but it's ugly.
>
> The StringEncoding proposal seems a bit much for most web use:
> http://wiki.whatwg.org/wiki/**StringEncoding<http://wiki.whatwg.org/wiki/StringEncoding>
>
> All we really ever do is work on DOMString, and that's covered by UTF8.
>

DOMString is not UTF8 or necessarily unicode.  It's a sequence of 16 bit
integers and a length.


>
> As following file shows, DOMString to ArrayBuffer conversion is about 30
> lines of code (start at line 125):
> http://code.google.com/p/**stringencoding/source/browse/**encoding.js<http://code.google.com/p/stringencoding/source/browse/encoding.js>


This only seems correct for valid unicode strings, which does not cover all
DOMStrings.

- James

>
>
> It seems like this kind of type conversion could be handled more
> efficiently and be less error prone on programmers like myself, who often
> forget to test with multibyte strings.
>
> I'm sure this has popped up many times before on the list. Thought I'd put
> it out there again.
> We could just tweak the ArrayBuffer constructor to support DOMString as an
> argument.
> Currently, it supports length.
>
> -Charles
>
>
Received on Wednesday, 11 January 2012 23:16:08 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:49 GMT