Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>(<strong>Update</strong> Please see the 2nd half of this answer, where I have (hopefully) provided a more complete solution.)</p> <p>I also ran into this issue, the following works for me in FF 6 (for one direction):</p> <pre><code>var buf = new ArrayBuffer( 10 ); var view = new Uint8Array( buf ); view[ 3 ] = 4; alert(Array.prototype.slice.call(view).join("")); </code></pre> <p>Unfortunately, of course, you end up with ASCII text representations of the values in the array, rather than characters. It still (should be) much more efficient than a loop, though. eg. For the example above, the result is <code>0004000000</code>, rather than several null chars &amp; a chr(4).</p> <p><strong>Edit:</strong></p> <p>After looking on <strong>MDC</strong> <a href="https://developer.mozilla.org/en/JavaScript_typed_arrays/Uint8Array">here</a>, you may create an <code>ArrayBuffer</code> from an <code>Array</code> as follows:</p> <pre><code>var arr = new Array(23); // New Uint8Array() converts the Array elements // to Uint8s &amp; creates a new ArrayBuffer // to store them in &amp; a corresponding view. // To get at the generated ArrayBuffer, // you can then access it as below, with the .buffer property var buf = new Uint8Array( arr ).buffer; </code></pre> <p>To answer your original question, this allows you to convert <code>ArrayBuffer</code> &lt;-> <code>String</code> as follows:</p> <pre><code>var buf, view, str; buf = new ArrayBuffer( 256 ); view = new Uint8Array( buf ); view[ 0 ] = 7; // Some dummy values view[ 2 ] = 4; // ... // 1. Buffer -&gt; String (as byte array "list") str = bufferToString(buf); alert(str); // Alerts "7,0,4,..." // 1. String (as byte array) -&gt; Buffer buf = stringToBuffer(str); alert(new Uint8Array( buf )[ 2 ]); // Alerts "4" // Converts any ArrayBuffer to a string // (a comma-separated list of ASCII ordinals, // NOT a string of characters from the ordinals // in the buffer elements) function bufferToString( buf ) { var view = new Uint8Array( buf ); return Array.prototype.join.call(view, ","); } // Converts a comma-separated ASCII ordinal string list // back to an ArrayBuffer (see note for bufferToString()) function stringToBuffer( str ) { var arr = str.split(",") , view = new Uint8Array( arr ); return view.buffer; } </code></pre> <p>For convenience, here is a <code>function</code> for converting a raw Unicode <code>String</code> to an <code>ArrayBuffer</code> (will only work with ASCII/one-byte characters)</p> <pre><code>function rawStringToBuffer( str ) { var idx, len = str.length, arr = new Array( len ); for ( idx = 0 ; idx &lt; len ; ++idx ) { arr[ idx ] = str.charCodeAt(idx) &amp; 0xFF; } // You may create an ArrayBuffer from a standard array (of values) as follows: return new Uint8Array( arr ).buffer; } // Alerts "97" alert(new Uint8Array( rawStringToBuffer("abc") )[ 0 ]); </code></pre> <p>The above allow you to go from <code>ArrayBuffer</code> -> <code>String</code> &amp; back to <code>ArrayBuffer</code> again, where the string may be stored in eg. <code>.localStorage</code> :)</p> <p>Hope this helps,</p> <p>Dan</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload