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:
<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 A but it is stored as 65 instead
Note
filesaver.min.js is a library that I use to save files from JS (Link on GitHub)