new Blob([str]) converts to utf8, but then you need to go through a
BlobReader or through an XHR+BlobURI to convert it to an arraybuffer.


On Wed, Jul 17, 2013 at 11:52 AM, Michal Mocny <notifications@github.com>wrote:

> In websocket-server/http.js:
>
> > +/**
> > + * Convert from an ArrayBuffer to a string.
> > + * @param {ArrayBuffer} buffer The array buffer to convert.
> > + * @return {string} The textual representation of the array.
> > + */
> > +var arrayBufferToString = function(buffer) {
> > +  return String.fromCharCode.apply(
> null, new Uint8Array(buffer));
> > +};
> > +
> > +/**
> > + * Convert a string to an ArrayBuffer.
> > + * @param {string} string The string to convert.
> > + * @return {ArrayBuffer} An array buffer whose bytes correspond to the string.
> > + */
> > +var stringToArrayBuffer = function(string) {
> > +  var buffer = new ArrayBuffer(string.length);
>
> I also use this 8-bit based arrays assumption in my sockets
> implementation, but javascript strings are really 16bit. I think direct
> 16bit->binary is usually not what the user expects (would receive 0-value
> bytes on the other end between each char), but I do think this could be a
> problem with some unicode input.
>
> I think the solution is to find a JS lib that converts a js-string into a
> UTF-8 encoded ArrayBuffer.
>
> —
> Reply to this email directly or view it on GitHub<https://github.com/GoogleChrome/chrome-app-samples/pull/122/files#r5245620>
> .
>