- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
文件读取器。 readAsBinaryString()返回 UTF-8 编码的二进制字符串。我如何才能获取作为一系列 Unicode 转义序列 (\uxxxx) 的数据?
最佳答案
FileReader.readAsBinaryString()
已弃用 - 改为使用 readAsArrayBuffer() .这允许您使用以下两种方法之一将输入字符串转换为转义的 unicode 字符:
这使用了 ArrayBuffer 和一个 Uint8Array View 。在下面的演示中,假设缓冲区已预加载(而是提供了一些虚拟数据)。
var buffer = new Uint8Array([0x20, 0xac, 0x2b, 0x08]), // big-endian format
pos = 0, txt = "";
// iterate buffer byte-per-byte and build string:
while(pos < buffer.length)
txt += "\\u" + toString(buffer[pos++]) + toString(buffer[pos++]);
// make sure we end up with two digits (v < 0x10)
function toString(v) {
var s = v.toString(16); return s.length === 1 ? "0" + s : s
}
out.innerHTML = txt;
<output id="out"></output>
如果数据是小端(在大多数主流系统上),您可以在这里使用 Uint16Array,并且只需将单个值转换为字符串而不是两个。或者使用 DataView 以便您可以阅读请求字节顺序。这可能会或可能不会稍微快一些(浏览器将进行字节交换,我们在一次操作中读取 16 位,但检查只是在 toString 方法中合并):
var data = new Uint8Array([0x20, 0xac, 0x2b, 0x08]), // big-endian format
view = new DataView(data.buffer), // use a view on the ArrayBuffer
pos = 0, txt = "";
// iterate buffer byte-per-byte and build string:
while(pos < view.byteLength) {
txt += "\\u" + toString(view.getUint16(pos, false)); // true = little endian
pos += 2
};
// make sure we end up with four digits
function toString(v) {
var s = v.toString(16);
return s.length === 3 ? "0" + s : (s.length === 2 ? "00" + s : s)
}
out.innerHTML = txt;
<output id="out"></output>
这使用新的 TextDecoder API 来解析输入缓冲区 - 这里也假定为 ArrayBuffer。
然后 escape
与替换一起使用。这是一种快速的转换方式,但是 escape()
也被弃用了。然而,它不会很快消失,所以如果你觉得大胆,它可能是一个选择——无论如何我都会把它包括在这里:
var td = new TextDecoder("utf-16be"), // be = big endian, def: le
buffer = new Uint8Array([0x20, 0xac, 0x2b, 0x08]); // big-endian format
// assumes data loaded into an ArrayBuffer
var txt = td.decode(buffer);
// escape is deprecated but won't go anywhere for a while:
out.innerHTML = escape(txt).replace(/%/g, "\\");
// or use the same last step as in method 1, just showing an alternative way
//=> "\u20AC\u2B08"
<output id="out"></output>
注意:您可能已经注意到我为字节顺序指定了大端。通常,在从网络读取文件或二进制数据时使用大端(又名网络顺序)。如果数据恰好是小端格式,则需要交换字节顺序:
对于方法 1,您可以:
while(pos < buffer.length) {
txt += "\\u" + toString(buffer[pos+1]) + toString(buffer[pos]);
pos += 2;
}
或者只是使用 Uint16Array 和上面提到的修改后的 toString 方法。
对于方法 2,您可以简单地为 utf-16 指定小端版本:
var td = new TextDecoder("utf-16"); // default = little-endian
请注意,TextDecoder
尚未稳定 nor supported in all browsers .
关于javascript - 将 FileReader.readAsBinaryString 转换为 Unicode 转义字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29007389/
第一次在这里发帖。 我正在编写一个 Java 程序,它接受一个输入文本文件,读取内容,然后将它们打印到屏幕上,同时创建一个包含内容的输出文件。我已经设置了必要的编写器,但是当我尝试使用 Buffere
类 FileReader 中的构造函数 FileReader 不能应用于给定类型; import java.io.*; public class FileReader{ publ
任何人都可以帮我修复我的代码,以便我可以编译它。 当我编译它时,会出现一条错误消息“对‘FileReader::FileReader()’的 undefined reference ”。 #ifnde
这是我的程序。我必须从 JFileChooser 选择一个文件,然后对其执行一些操作。问题是,在我选择文件之前,filereader 给出了 nullpointer ,说实话,我什至无法选择,因为当
我无法真正理解 Java 中 FileReader 和 BufferedReader 类的用途。 在 docs.oracle 中,建议将缓冲读取器包装在 FileReader 对象周围,因为直接使用
我正在尝试读取 CSV 或 Excel 文件以将其转换为表格。现在我刚刚创建了一个类来读取输入文件: 输入是: 现在我刚刚创建了一个类来读取输入文件: class InputReader_tiqe
我正在玩弄食物识别 API。 我有一个名为 ingredients 的本地状态组件。 在组件中,我有一个 input 标签,它接受文件图像上传并调用 cameraHandler 方法 onChange
我正在尝试使用此应用脚本将文件上传到 Google 云端硬盘,它适用于较小的文件,但在上传大于约 1 GB 的文件时失败 + 一旦加载大文件,onerror 函数就会执行。我无法弄清楚如何在切片中读取
我的代码 - $selectFile = $(''); $selectFile.click(function () { this.value = null; });
我有一个 FileReader,可以让用户将文件(图像)上传到我的网站。 这是执行读取的代码: $("input[type='file']").change(function(e) { var
我知道异步方法应该是非阻塞的。但我通常看到它们应用于诸如 fetch() 之类的外部操作。 即:在浏览器外部处理的内容。 但是 FileReader() API 又如何呢?文件处理是由浏览器完成的,对
我有(可能)很容易解决的问题,但我自己无法解决。基本上我的reader.width和reader.height一直说未定义:/..其他一切都按预期工作 function handleFileSelec
我想像asp.net一样获取图像文件流并将它们传递到后台,但是每次我尝试触发onload事件时,它总是在编程通过后完成。 我尝试使用setTimeout来阻止它并让它处理并等待它成功,但它失败了。下面
我有两个类。首先是 TEXT 类:这里我读取了一个 6 行的文本文件。我只想逐行阅读,但这可行。但我想从第三行开始,并跳过最后一行,我只希望以 这是文本文件代码。 这是文件读取器的代码:
我仍在学习过程中,如果我有误解,请纠正我,但 FileReader 对象不应该返回文本文件的全部内容吗? 我这里有一段代码,我简单地尝试获取一个短.txt文件的内容,并使用system.out.pri
我收到错误“未处理的异常:java.io.FileNotFoundException,虽然我确定文件路径,但需要一些帮助 public static void main(String[] args)
我有一个我看不到的简单问题,希望另一双眼睛可以帮助我解决。除了我认为的文件阅读器之外,一切都运行良好。我应该让用户输入将其放入文件中,读取该文件并执行正确的操作。但由于某种原因,它只对用户输入的最后一
我尝试使用java.io.FileReader读取一些文本文件并将它们转换为字符串,但我发现结果编码错误并且根本不可读。 这是我的环境: Windows 2003,操作系统编码:CP1252 Java
在java中使用FileReader方法,使文件只读还是仍然需要声明 FileReader file = new FileReader(args[0]); BufferedReader buff
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭10 年前。 Improve th
我是一名优秀的程序员,十分优秀!