Re: String to ArrayBuffer

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 UTC