Search code examples
binaryadodbjscriptwsh

How to write a sequence of bytes to binary file using JScript?


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?


Solution

  • 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:

    enter image description here

    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