Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Although Dennis and gengkev solutions of using Blob/FileReader work, I wouldn't suggest taking that approach. It is an async approach to a simple problem, and it is much slower than a direct solution. I've made a post in html5rocks with a simpler and (much faster) solution: <a href="http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String">http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String</a></p> <p>And the solution is:</p> <pre><code>function ab2str(buf) { return String.fromCharCode.apply(null, new Uint16Array(buf)); } function str2ab(str) { var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char var bufView = new Uint16Array(buf); for (var i=0, strLen=str.length; i&lt;strLen; i++) { bufView[i] = str.charCodeAt(i); } return buf; } </code></pre> <hr> <p><strong>EDIT:</strong></p> <p>The <a href="http://updates.html5rocks.com/2014/08/Easier-ArrayBuffer---String-conversion-with-the-Encoding-API">Encoding API helps solving the string conversion</a> problem. Check out the response from <a href="http://www.html5rocks.com/profiles/#jeffposnick">Jeff Posnik</a> on Html5Rocks.com to the above original article.</p> <p>Excerpt:</p> <blockquote> <p><em>The Encoding API makes it simple to translate between raw bytes and native JavaScript strings, regardless of which of the many standard encodings you need to work with.</em></p> </blockquote> <pre><code>&lt;pre id="results"&gt;&lt;/pre&gt; &lt;script&gt; if ('TextDecoder' in window) { // The local files to be fetched, mapped to the encoding that they're using. var filesToEncoding = { 'utf8.bin': 'utf-8', 'utf16le.bin': 'utf-16le', 'macintosh.bin': 'macintosh' }; Object.keys(filesToEncoding).forEach(function(file) { fetchAndDecode(file, filesToEncoding[file]); }); } else { document.querySelector('#results').textContent = 'Your browser does not support the Encoding API.' } // Use XHR to fetch `file` and interpret its contents as being encoded with `encoding`. function fetchAndDecode(file, encoding) { var xhr = new XMLHttpRequest(); xhr.open('GET', file); // Using 'arraybuffer' as the responseType ensures that the raw data is returned, // rather than letting XMLHttpRequest decode the data first. xhr.responseType = 'arraybuffer'; xhr.onload = function() { if (this.status == 200) { // The decode() method takes a DataView as a parameter, which is a wrapper on top of the ArrayBuffer. var dataView = new DataView(this.response); // The TextDecoder interface is documented at http://encoding.spec.whatwg.org/#interface-textdecoder var decoder = new TextDecoder(encoding); var decodedString = decoder.decode(dataView); // Add the decoded file's text to the &lt;pre&gt; element on the page. document.querySelector('#results').textContent += decodedString + '\n'; } else { console.error('Error while requesting', file, this); } }; xhr.send(); } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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