This should solve your problem.
How to use strings with typed JavaScript arrays
JS strings are stored in UTF-16 encoding, where each character takes 2 bytes. String.charCodeAt returns these 2-byte Unicodes. Here's how to read UTF-16 encoded strings from a DataView:
DataView.prototype.getUTF16String = function(offset, length) { var utf16 = new ArrayBuffer(length * 2); var utf16View = new Uint16Array(utf16); for (var i = 0; i < length; ++i) { utf16View[i] = this.getUint8(offset + i); } return String.fromCharCode.apply(null, utf16View); };
and these functions to convert strings to and from arraybuffer
function ab2str(buf) { return String.fromCharCode.apply(null, new Uint16Array(buf)); } function str2ab(str) { var buf = new ArrayBuffer(str.length*2);
source share