Read UTF-8 special chars from external file using

2019-08-29 09:42发布

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

1条回答
一纸荒年 Trace。
2楼-- · 2019-08-29 10:16

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.

查看更多
登录 后发表回答