0

I have a UTF-8 encoded file "myFile.aaa" with non-printable char represented by hexadecimal x80 (decimal 128).

I need to develop a Javascript function that will read this char from myFile.aaa and return its decimal value, 128.

Is it possible to do that? How?

If I copy myFile.aaa content to "var data", and do "data[0].charCodeAt(0)" I get value 8364 instead of 128.

Thanks

  • 2
    A freestanding decimal 128 is not a valid UTF-8 character. Where does it come from? – Pekka Feb 19 '12 at 23:08
  • If I need to write in a file a byte representing decimal 128, what char encoding should I use, so that I can easily read it from Javascript? Thanks –  Feb 19 '12 at 23:21

1 Answers1

0

I don't think your UTF-8 encoding makes sense, so I'm going to tell you the best way I've found of dealing with dodgy AJAX data. Set the content type as user-defined:

var req = new XMLHttpRequest();
req.overrideMimeType('text/plain; charset=x-user-defined')

You can then just read the file as plain bytes instead of encoded characters.

Jivings
  • 22,834
  • 6
  • 60
  • 101
  • I really need to have a byte representing decimal 128 in the file to be read, doesn't matter what encoding I am using (I can change UTF-8 for any other encoding that makes sense for you). Will then your code work the same without any changes on myFile.aaa? Thanks –  Feb 19 '12 at 23:42
  • I think it should do. You just need to know how many bytes your chars represent. – Jivings Feb 20 '12 at 09:57