使用Javascript从外部文件读取UTF-8特殊字符
我有一个UTF-8编码的文件"myFile.aaa",其不可打印的字符由十六进制x80(十进制128)表示.
I have a UTF-8 encoded file "myFile.aaa" with non-printable char represented by hexadecimal x80 (decimal 128).
我需要开发一个Javascript函数,该函数将从myFile.aaa中读取此char并返回其十进制值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?
如果将myFile.aaa内容复制到"var data
",然后执行"data[0].charCodeAt(0)
",则得到的值是8364,而不是128.
If I copy myFile.aaa content to "var data
", and do "data[0].charCodeAt(0)
" I get value 8364 instead of 128.
谢谢
我认为您的UTF-8编码没有意义,因此,我将告诉您我找到的处理狡猾AJAX的最佳方法数据.将内容类型设置为用户定义:
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.