How to write a sequence of bytes to binary file using JScript? - binary
I'm trying to write Array(1, 2) to binary file as bytes.
So output file should contain 00000001 00000010.
I understood that I have to use ADODB.Stream, but I haven't found any solution for such simple task.
var data = new Array(1, 2)
out = WScript.CreateObject("ADODB.Stream")
out.Type = 1
out.Open()
out.Write(data)
out.SaveToFile("output.bin", 2)
out.Close()
Code above gives error:
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
Any ideas how to convert data array to acceptable type?
You can use the WriteAll function described here: Reading and Writing Binary Files Using JScript. Copyright: Dr Alexander J Turner.
Please note, the Stream shall be of type text, and then you will need to do the text<->binary conversion.
I just tested all the code and it worked fine for me.
Here is a complete example:
var bf1 = new BinaryFile("C:/Temp/test.bin");
var outBuf = '';
for(var i=0, l=data.length; i<l; i++) {
outBuf += String.fromCharCode(data[i]);
}
bf1.WriteAll(outBuf);
The result looks like this:
EDIT:
I just did a more compact adaptation of the code, to avoid the hex conversion and the double loop:
// Codepage conversion table
var _c=[199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,
201,230,198,244,246,242,251,249,255,214,220,162,163,165,8359,402,
225,237,243,250,241,209,170,186,191,8976,172,189,188,161,171,187,
9617,9618,9619,9474,9508,9569,9570,9558,9557,9571,9553,9559,9565,9564,9563,9488,
9492,9524,9516,9500,9472,9532,9566,9567,9562,9556,9577,9574,9568,9552,9580,9575,
9576,9572,9573,9561,9560,9554,9555,9579,9578,9496,9484,9608,9604,9612,9616,9600,
945,223,915,960,931,963,181,964,934,920,937,948,8734,966,949,8745,
8801,177,8805,8804,8992,8993,247,8776,176,8729,183,8730,8319,178,9632,160],
_w=[],
_r=[];
// Create forward lookup to write & reverse lookup to read
for(var i=0, l=256; i<l; i++) {
var c = (i<128) ? i : _c[i-128];
_w[i] = c;
_r[c] = i;
}
// Read binary data from disk
function binFileToArray(fileName, binArray){
var inStream = new ActiveXObject("ADODB.Stream");
inStream.Type = 2;
inStream.CharSet = '437';
inStream.Open();
inStream.LoadFromFile(fileName);
var inString = inStream.ReadText();
inStream.Close();
for(var i=0, l=inString.length; i<l; i++) {
binArray.push(_r[inString.charCodeAt(i)]);
}
}
// Write binary data to disk
function binArrayToFile(binArray, fileName){
var outStream = new ActiveXObject('ADODB.Stream');
var outString = '';
for(var i=0, l=binArray.length; i<l; i++) {
outString += String.fromCharCode(_w[binArray[i]]);
}
outStream.Type = 2;
outStream.CharSet = '437';
outStream.Open();
outStream.WriteText(outString);
outStream.SaveToFile(fileName, 2);
outStream.Close();
}
// TEST: read binary file from disk & write array back to disk
var testArray = [];
binFileToArray('c:/temp/test.bin', testArray);
binArrayToFile(testArray, 'c:/temp/test2.bin');
// both files equals
Related
How to merge arrays by index AS3
Seeking a way to merge arrays by index like so: var a1:Array = ["a", "b", "c"]; var a2:Array = ["1", "2", "3"]; var a3:Array = result: ["a", 1, "b", 2, "c", 3] I've tried concat, push, splice... not getting close. Thanks.
function arrayMerge(arr1:Array, arr2:Array):Array { var o:Array = new Array(); for (var i:int=0; i<Math.max(arr1.length, arr2.length); i++) { if (i<arr1.length) o.push(arr1[i]); if (i<arr2.length) o.push(arr2[i]); } return o; }
Thanks Payam for answer and non-judgement. Here's how I applied your work: var arr1:Array = ["question1", "question2", "question3"]; var arr2:Array = ["answer1", "answer2", "answer3"]; var o:Array = new Array(); for (var i:int=0; i<Math.max(arr1.length, arr2.length); i++) { if (i<arr1.length) o.push(arr1[i]); if (i<arr2.length) o.push(arr2[i]); } trace(o); //(question1,answer1,question2,answer2,question3,question3)
#AndyH : payamsbr is right, but you may work with Vectors or Arrays Perhaps tl; dr; but this is the principle. If You want to understand something try those possibilities. If you don't, just copy and paste some shorter code ;) var v1:Vector.<String> = new <String>["a", "b", "c"]; var v2:Vector.<uint> = new <uint>[1, 2, 3]; // why do you use String here and not uint? // if you want to convert a uint to a String, use myUint.toString(); function convertVectorToArray(v1:Vector.<String>,v2:Vector.<uint>):Array{ var mergedArray:Array = new Array(); if (v1.length != v2.length){ throw(new Error(" ***ERROR : the two Vectors or Arrays have not the same lenfgth!")); }else{ for(var i:uint = 0; i <v1.length ; i++){ mergedArray.push(v1[i]); mergedArray.push(v2[i]); } } return(mergedArray); } function mergeVectors(v1:Vector.<String>,v2:Vector.<uint>):Vector.<Object>{ var mergedVector:Vector.<Object> = new Vector.<Object>(); if (v1.length != v2.length){ throw(new Error(" ***ERROR : the two Vectors or Arrays have not the same length!")); } for(var i:uint = 0; i <v1.length ; i++){ mergedVector.push(v1[i] as String); mergedVector.push(v2[i] as uint); } return(mergedVector); } var mergedArray:Array = (convertVectorToArray(v1,v2)); var mergedVector:Vector.<Object> = (mergeVectors(v1,v2)); function listArray(arr:Array):String{ var str: String=""; if ((v1.length*2) != (v1.length + v2.length)){ throw(new Error(" ***ERROR : the two Vectors or Arrays have not the same length!")); }else{ for (var i:uint = 0; i < arr.length ; i++){ str+="typeof(arr[" + i + "]) = " + (typeof(arr[i]) as String).toUpperCase() + ", value = " + arr[i] + "\n"; } } return str; } function listVector(vect:Vector.<Object>):String{ var str: String = ""; if ((v1.length*2) != (v1.length + v2.length)){ throw(new Error(" ***ERROR : the two Vectors or Arrays have not the same length!")); }else{ for (var i:uint = 0; i < vect.length ; i++){ str+="typeof(vect[" + i + "]) = " + (typeof(vect[i]) as String).toUpperCase() + ", value = " + vect[i] + "\n"; } } return str; } trace(listArray(mergedArray)); trace(listVector(mergedVector)); You may add a sort() method if You need it (you didn't told about it) And Always throw an Error if the 2 Arrays or Vectors don't have the same length! Throwing an Error is the best way to understand if something goes wrong... This will avoid You a lot of time if You need to debug Your code!!! As You can see the output is the same, but if the Vector Class is used correctly, this is more efficient than an Array. Output : Since there's a Vector Class, I don't understand a lot of people who chose Arrays instead... Of course Vector. is a nonsense, but I posted it anyway so You can figure You out the Vector Class. Output is the same : typeof(arr[0]) = STRING, value = a typeof(arr[1]) = NUMBER, value = 1 typeof(arr[2]) = STRING, value = b typeof(arr[3]) = NUMBER, value = 2 typeof(arr[4]) = STRING, value = c typeof(arr[5]) = NUMBER, value = 3 typeof(vect[0]) = STRING, value = a typeof(vect[1]) = NUMBER, value = 1 typeof(vect[2]) = STRING, value = b typeof(vect[3]) = NUMBER, value = 2 typeof(vect[4]) = STRING, value = c typeof(vect[5]) = NUMBER, value = 3 I forgot this easiest way if you really want an Array... Quick done! var ar1:Array = [1,2,3]; var ar2:Array = ["a","b","c"]; function merge(...arrays):Array { var result:Array = []; for(var i:int=0;i<arrays.length;i++){ result = result.concat(arrays[i]); } return result; } trace(merge(ar1, ar2)); // outputs : 1,2,3,a,b,c Another possibility : function populateObject(v1:Vector.<String>, v2:Vector.<uint>):Object{ var obj = new Object(); if ((v1.length*2) != (v1.length + v2.length)){ throw(new Error(" ***ERROR : the two Vectors or Arrays have not the same length!")); }else{ for (var i:uint = 0; i < v1.length; i++){ obj[v2[i]] = v1[i]; } } return obj; } var o:Object = populateObject(v1,v2); function listObject(someObj:Object):void{ var myObj:Object = someObj; for (var i:String in someObj){ trace(someObj[i] + ": " + i); } } listObject(o); output = a: 1 b: 2 c: 3 I think that You have a lot of possibilities to use here even it's my longer answer ;) If You try those possibilities and understand them, this will certainty help You to think to find the best way to deal with Your issue. But You may also copy and paste some shorter code. I just wanted to show You that there's more than one answer. If you understand this, You will be able to go further with coding. Have fun ;) Sincerely. Nicolas Best regards. Nicolas.
efficient way to insert following integers to a vector
Is there a more efficient way to do : var firstItemIndex:int = 220; var lastItemIndex:int = 880; var indices:Vector.<int> = new Vector.<int>(); for(var i:int = firstItemIndex; i <= lastItemIndex; i++) { indices.push(i); } Short explanation - is there a more efficient way to insert integers from StartNumber to EndNumber without having to loop ?
I'm not completey sure how AS3 handles memory allocation under the hood, but it may be more efficient to make a fixed length vector instead of a variable one: var firstItemIndex:int = 220; var lastItemIndex:int = 880; var indices:Vector.<int> = new Vector.<int>(lastItemIndex - firstItemIndex + 1, true); function setItem(item:int, index:int, vector:Vector.<int>):void{ vector[index] = firstItemIndex + index; } indices.forEach(setItem);
Get each element from array as string actionscript3
I have an array like var test:Array = new Array("a", "b", "c"); How can I write a method to get one element and make it be string each time when I call this method. i.e when I call the method, it should return only 'a' and next time return only 'b' and so on.
You can use function shift of Array,here is a link about the function array shift var test:Array = new Array("a", "b", "c"); var firstLetter:String = test.shift();//"a" var secondLetter:String = test.shift();//"b" var thirdLetter:String = test.shift();//"c"
#Pan's answer is correct, but I feel the need to flag the fact that shift() ignites an extremely slow process of re-indexing your entire array. It's not something you need to concern yourself with with small arrays like in your example, but for larger arrays there's a significant performance boost if you reverse() the array first and then use pop(). I'll create a performance comparison below. Set up our test arrays: var shiftCopy:Array = []; var popCopy:Array = []; for(var i:int = 0; i < 100000; i++) { var rand:Number = Math.random() * i; shiftCopy.push('a' + rand); popCopy.push('a' + rand); } Run the tests: // Using shift. var t:int = getTimer(); while(shiftCopy.length > 0) shiftCopy.shift(); trace(getTimer() - t); // Using reverse and pop. t = getTimer(); popCopy.reverse(); while(popCopy.length > 0) popCopy.pop(); trace(getTimer() - t); My results: shift: 1651ms pop: 19ms
Store Series of 0s and 1s in MySQL database
I'm using MySql and Nodejs. I need to store a huge string made of only 0s and 1s. (This is a map grid. 0 = Can Move, 1 = Can't move.) Ex: 00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000 ... Should I take that string, encode it into a base64 string and store it that way? Then when I need it, I decode it? If so, how would I do it, using Nodejs/Javascript? Thanks
The data you're storing appears to compress easily, why don't you use the built-in MySql COMPRESS function Raw: mysql> select length('00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000'); 332 With COMPRESS mysql> select length(compress('00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000')); 23
Don't store a string, store binary. Here's an example of a way you could convert that data to binary. Hopefully you can reengineer things so that your map data is never represented as a string in the first place. static void Main(string[] args) { const string data = "00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000"; var arrayLength = data.Length/8; if (data.Length%8 != 0) arrayLength++; var binaryData = new byte[arrayLength]; byte nextByte=0; var k = -1; for (var i = 0; i < data.Length; i++) { var j = i%8; if (j == 0) { binaryData[++k] = nextByte; nextByte = 0; } var bit = int.Parse(data[i].ToString()); if(bit==1) nextByte |= (byte)(bit << j); } binaryData[k] = nextByte; }
Read set of numbers from txt file
I've been working on a project for a while but got stuck where I have a text file that contains a set of numbers in this format: 1-2-3-4 1-2-3-4 1-2-3-4 1-2-3-4 So I must read the numbers from the file and put them in an array according to the column so at the end I have column1:Array (1,1,1,1) column2:Array (2,2,2,2) ..... and so on. I can't figure how to do this. What I managed to do was read all the file and have all the numbers in 1 array but just that. Here's the code var myTextLoader:URLLoader = new URLLoader; var txtArray:Array; myTextLoader.load(new URLRequest(inputFile.text)); myTextLoader.addEventListener(Event.COMPLETE,onLoaded); function onLoaded(e:Event):void { txtArray = e.target.data.split(/\-|\n/g); }
before split \n \r to array (reading with a loop) and the same with... - a loop in loop to get a multidimensional array in mind the result is a "table" finally to get a result do this. variable[file][column] a[2][3] ----> 4 ;)
Try splitting it into two parts, first by line, then by element: function onLoaded(e:Event):void { preArray:Array = e.target.data.split(/\n/g); txtArray = new Array(); for(var i:int = 0; i < preArray.length; i++) { txtArray.push(preArray[i].split(/\-/g)); } } This will give you a 2D array, which you'd access like this: textArray[0][0]; // result: 1 textArray[2][3]; Et cetera.
Thanks to Llanis who gave me the idea. This is my final code. Sorry i didn't tag the language it never ocurred to me i'm new. myTextLoader.load(new URLRequest(inputWX.text)); myTextLoader.addEventListener(Event.COMPLETE,onLoaded); function onLoaded(e:Event):void { txtArray = e.target.data.split(/\-|\n/g); var wArray:Array = new Array(txtArray.length/4); var xArray:Array = new Array(txtArray.length/4); var yArray:Array = new Array(txtArray.length/4); var zArray:Array = new Array(txtArray.length/4); var a:int = 0; var b:int = 0; var c:int = 0; var d:int = 0; var columna:int = 1; for(var arrayIndex:int = 0; arrayIndex <= txtArray.length-1;arrayIndex++) { switch(columna){ case 1: wArray[a] = txtArray[arrayIndex]; a+=1; break; case 2: xArray[b] = txtArray[arrayIndex]; b+=1; break; case 3: yArray[c] = txtArray[arrayIndex]; c+=1; break; case 4: zArray[d] = txtArray[arrayIndex]; d+=1; break; } if(columna == 4) columna = 1; else columna++; } }