0

I have a file that's encoded with UTF-16 and the encoded string has characters such as à and ÷. The code I use to read the file is:

  var reader = new FileReader();
  reader.readAsArrayBuffer(real_file_button.files[0]);
  reader.onloadend = function (evt) {
      if (evt.target.readyState == FileReader.DONE) {
         var arrayBuffer = evt.target.result,
             array = new Uint8Array(arrayBuffer);
         for (var i = 0; i < array.length; i++) {
             fileByteArray.push(array[i]);
          }
      }
  }

but since it is reading it as UTF-8 the à and ÷ characters get converted to . How can I get a byte array of the file while keeping the correct encoding?

2
  • "but since it is reading it as UTF-8" - um, no? It is reading the file into a raw buffer of bytes, there are no strings and no encoding involved in the code you've shown. Where exactly do you output any characters? Commented Nov 21, 2019 at 1:04
  • Btw, what is fileByteArray, what's the point of copying the array you already have into it? Commented Nov 21, 2019 at 1:05

1 Answer 1

2

There is nothing doing any conversion to UTF-8 here, you are just copying the numerical values from the TypedArray view to a normal Array (which seems quite pointless btw...).

Not sure what you call a "byte array", but if you want to read this binary data as an UTF-16 string, then use the readAsText( blob, encoding ) method of the FileReader:

const file = real_file_button.files[0];
reader.onload = (e) => doSomethingWith( reader.result );
reader.readAsText( file, "utf-16" )

This will default to little endian utf-16. If you need big endian, use "utf-16be".

If you wish to view your ArrayBuffer as uint16 values, then create an Uint16Array view of it:

reader.readAsArrayBuffer( file );
reader.onload = (e) => {
  const uint16view = new Uint16Array( reader.result );
  // doSomethingWith( uint16view )
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.