Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is what I'm trying to implement:
- //
- // A function which takes raw data (ArrayBuffer) and
- // returns the data as a raw "binary" string,
- // just as you would get if you used FileReader.readAsBinaryString().
- function BytesToString(bytes) {
- return String.fromCharCode.apply(null, new Uint8Array(bytes));
- }
- // Working with a small buffer works fine:
- const smallData = new Uint8Array([
- 72, // 'H'
- 101, // 'e'
- 108, // 'l'
- 108, // 'l'
- 111, // 'o'
- 33, // '!'
- 0, // raw byte 0x00
- 238, // raw byte 0xEE
- ]);
- console.log(smallData);
- console.log(BytesToString(smallData));
- // The problem is it falls apart for larger buffers,
- // even if the size is still "reasonable" (2**20 == one megabyte)
- //
- // In Chrome this results in the somewhat-misleading error,
- // "Unhandled Rejection (RangeError): Maximum call stack size exceeded".
- //
- // This is disabled in order to show the rest of the example without a crash.
- // Change "false" to "true" to enable it:
- const showIssue = false;
- if (showIssue) {
- var bigData = new ArrayBuffer(2**20);
- var bigDataBytes = new Uint8Array(bigData);
- for (var i = 0; i < bigData.byteLength; ++i) {
- bigDataBytes[i] = 88; // 'X'
- }
- console.log(bigDataBytes);
- console.log(BytesToString(bigData));
- }
- // As an alternative I tried to use TextEncoder/TextDecoder, but
- // the encoder thinks the data should be written as a list of numbers
- // and not as a raw string. (I don't really like this method anyway
- // because it introduces a needless encoding to UTF-8 and then
- // decoding back to the original code points.)
- var encoder = new TextEncoder();
- var decoder = new TextDecoder();
- const encoding = encoder.encode(smallData);
- const decoding = decoder.decode(encoding);
- console.log(encoding);
- console.log(decoding);
- // I can't simply use TextDecoder alone, because it mis-interprets
- // the input as being UTF-8 encoded. Because the raw byte 0xEE is not
- // a valid UTF-8 sequence, the last character is replaced by the "Unicode
- // replacement character (�)":
- console.log(decoder.decode(smallData));
Add Comment
Please, Sign In to add comment