Read UTF-8 special chars from external file using Javascript

你离开我真会死。 提交于 2019-12-11 18:17:57

问题


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:


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.



来源:https://stackoverflow.com/questions/9354213/read-utf-8-special-chars-from-external-file-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!