jswin icon indicating copy to clipboard operation
jswin copied to clipboard

Safer ab2str

Open PAEz opened this issue 12 years ago • 0 comments

I encountered a problem where the buffer was to big (~34k) and using the normal code got me a Maximum call stack exceeded (think thats what it was) error. So I came up with the following.

function ab2str(buf) {
    if (buf.byteLength == 0) return ""
    var bufView = new Uint8Array(buf);
        if (bufView[0]==0) return "";
    var unis = [];
    var step = 32768;
    var str = "";
    unis.push(bufView[0]);
    for (var i = 1; i < bufView.length; i++) {
        if (bufView[i] == 0) break;
        unis.push(bufView[i]);
        if (i % step == 0) {
            str += String.fromCharCode.apply(null, unis);
            unis = [];
        }
    }
    str += String.fromCharCode.apply(null, unis);
    return str;
}

PAEz avatar Nov 28 '13 12:11 PAEz