在没有charCodeAt的情况下将字符串转换为UInt8Array

时间:2014-11-08 23:12:59

标签: firefox-addon

我听说使用charCodeAt将字符串转换为UInt8Array非常慢我试图用nsIStringInputStream找出一种方法,我认为这种方式最简单?

所以我试图避免这个:

var str = atob('rawr');
var data = new Uint8Array(str.length);
for (var i = 0, e = str.length; i < e; ++i) {
  data[i] = str.charCodeAt(i);
}

我不确定我是否做得对,请告知:

var {Constructor: CC} = Components;
var BinaryInputStream = CC('@mozilla.org/binaryinputstream;1', 'nsIBinaryInputStream', 'setInputStream');
var StringInputStream = CC('@mozilla.org/io/string-input-stream;1', 'nsIStringInputStream', 'setData');
var str = atob('rawr');
var _inputStreamScriptable = new StringInputStream(str, str.length);
var _inputStreamBinary = new BinaryInputStream(_inputStreamScriptable);
var data = new Uint8Array(str.length);
data.set(_inputStreamBinary.readByteArray(str.length));

感谢

0 个答案:

没有答案