将字节数组写入二进制文件 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26244126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing Byte Array To Binary File Javascript
提问by Ahmad Hajjar
I am trying to write a script that generates random numbers these number then are converted to groups of 4Bytes each then these 4-bytes-groups are put into an Uint8Array finally I try to save the array to binary file. Here is my code:
我正在尝试编写一个生成随机数的脚本,然后将这些数字转换为每个 4 字节的组,然后将这些 4 字节的组放入 Uint8Array 最后我尝试将数组保存到二进制文件。这是我的代码:
<html>
<head>
<title>Raandom Number Generator</title>
</head>
<body>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="filesaver.min.js"></script>
<script type="text/javascript">
$(function() {
if (!Date.now) {
Date.now = function() {
return new Date().getTime();
};
}
alert("started");
var currentMousePos = {
x : -1,
y : -1
};
var randomData = [];
var bytes = new Int8Array(4);
$(document).mousemove(function(event) {
if(randomData.length>=1231){
$(document).unbind('mousemove');
console.log("Finished generating numbers ... trying to save file");
randomData = new Uint8Array(randomData);
var blob = new Blob(randomData, {type: "application/octet-stream"});
saveAs(blob, "rand.bin");
return;
}
currentMousePos.x = event.pageX;
currentMousePos.y = event.pageY;
var longRandomNumber = Date.now() * (event.pageX + 1)
* (event.pageY + 1);
for ( var i = 3; i >= 0; i--) {
bytes[i] = longRandomNumber & (255);
longRandomNumber = longRandomNumber >> 8
}
for ( var i = 0; i < 4; i++) {
randomData.push(bytes[i]);
}
console.log(randomData.length);
});
})
</script>
</body>
</html>
The problem is that the generated file contains numbers plain numbers
for example an element in the Uint8Array may be 65, in my understanding to binary this value should be saved as capital letter Abut it is stored as 65instead
问题是,所生成的文件包含数字plain numbers
,例如在Uint8Array的元素可以是65,在我的理解为二进制此值应保存为大写字母一个,但它被存储为65,而不是
Note
笔记
filesaver.min.js is a library that I use to save files from JS (Link on GitHub)
filesaver.min.js 是一个库,我用来保存来自 JS 的文件(GitHub 上的链接)
回答by Bergi
The Blob
constructordoes expect an arrayof typed arrays to be concatenated, but you are passing only a single Uint8Array
into it. This will probably be interpreted as (should I say, "casted to"?) an array of DOM-strings - that's where your numbers are coming from.
该Blob
构造是否期待一个数组类型数组的要连接,但只有经过一个Uint8Array
进去。这可能会被解释为(我应该说,“转换为”吗?)一个 DOM 字符串数组——这就是你的数字的来源。
A quickfix would be to use
一个快速修复将使用
new Blob([randomData], {type: "application/octet-stream"})
// ^ ^
but I would suggest to either do
但我建议要么做
var randomData = [];
// while randomData.length < 308
var bytes = new Uint8Array(4);
for (var i=4; i--; ) {
bytes[i] = longRandomNumber & (255);
longRandomNumber = longRandomNumber >> 8
}
randomData.push(bytes);
var blob = new Blob(randomData, {type: "application/octet-stream"});
or not use those 4-byte bytes
arrays at all:
或者根本不使用那些 4 字节bytes
数组:
var randomData = new Uint8Array(1232),
count = 0;
// while count < randomData.length
for (var i=4; i--; ) {
randomData[count++] = longRandomNumber & (255);
longRandomNumber = longRandomNumber >> 8
}
var blob = new Blob([randomData], {type: "application/octet-stream"});