使用Javascript从外部文件读取UTF-8特殊字符 [英] Read UTF-8 special chars from external file using Javascript

查看:258
本文介绍了使用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.

这篇关于使用Javascript从外部文件读取UTF-8特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆