简体   繁体   English

这个JS代码在做什么

[英]what's this JS code doing

What is that code doing? 该代码在做什么?

var buf = new ArrayBuffer(fileContent.length);
var view = new Uint8Array(buf);
for (var i=0; i!=fileContent.length; ++i)
    view[i] = fileContent.charCodeAt(i) & 0xFF;

in context of function of FileSaver.js 在FileSaver.js函数的上下文中

saveAs(new Blob([buf],{type:""}), filename);

I cant get the entire goal of this code (except the last string, that saves the buf), cant get why are we changing view if we're saving buf , and how is view corresponds to the buf , and what is that fileContent.charCodeAt(i) & 0xFF command means? 我无法获得这段代码的全部目标(最后一个字符串除外,它保存了buf),如果我们要保存buf ,为什么无法更改视图 ,以及视图如何对应于buf ,以及fileContent是什么。 charCodeAt(i)和0xFF命令是什么意思?

charCodeAt(i) gets the i th character of the fileContent string, and returns its character code as a number. charCodeAt(i)获取fileContent字符串的第i个字符,并以数字形式返回其字符代码。 & 0xFF performs a binary AND operation between that number and the hexidecimal number 0xFF ; & 0xFF在该数字和十六进制数字0xFF之间执行二进制AND运算; which masks it to the low-order 8 bits. 将其屏蔽为低阶8位。 Then this stores that 8-bit number into view[i] . 然后,它将那个8位数字存储到view[i]

So this whole thing is essentially copying the string from fileContent to buf , but stripping off any high-order bits to get 8-bit characters. 因此,整个过程本质上是将字符串从fileContent复制到buf ,但剥离所有高阶位以获得8位字符。

The explicit masking here is not really necessary. 此处的显式掩盖并不是必须的。 Assigning to a Uint8Array automatically assigns the value modulo 256. 分配给Uint8Array自动将值取模256。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM