Reading binary files

Hello,
I need to read out a binary file in my Gadget.
In this forum, I asked before, how I can read out files in general. Now I've got the next problem

The whole code for reading the file looks like this:

var oFSO = new ActiveXObject('Scripting.FileSystemObject');
var fTmp = oFSO.OpenTextFile(filename, 1, false, 0);
var content = fTmp.ReadAll();
fTmp.Close();
var byteString = "";

for(i = 0; i < content.length; i++)
{
byteString = byteString + hex(content.charCodeAt(i));
}

.....................

function hex(a)
{
return Number(a).toString(16);
}

I need a String that contains the file in this form: "AABBCC66339953AB..."
I need it for sending it to a XML RPC. But I'm not able to figure it out.
It works in general. But not all the characters in the string are ASCII characters. I think that the table (if a table is used - I think so) that is used by charCodeAt() only contains ASCII characters and because of this it isn't converted in the right way.

Is there any possibility to get the hexadecimal data of the string to another string Perhaps reading a ByteArray or such things DIRECTLY from the file. Not a String...

Thanks for your help!


Answer this question

Reading binary files

  • StUdEnT in distress

    Use an ADO stream instead:

    var adTypeBinary = 1

    oStream = new ActiveXObject("ADODB.Stream");
    oStream.Type = adTypeBinary;
    oStream.Open;

    oStream.LoadFromFile(filename);
    content = oStream.Read;

    oStream.Close;
    oStream = null;

  • Brad_SAN

    I'd have thought my example would work then... you're just adding to a string based on whether or not the current character is one of the characters in the test string.


  • ahmad.nazmi

    Sorry my javascript knowledge is next to nothing, but couldn't you use indexof

    var hexchars = "0123456789ABCDEF";

    for(i = 0; i < content.length; i++)
    {
        if (IndexOf(tostring.
    hex(content.charCodeAt(i)) !=-1)
            {
             byteString = byteString + hex(content.charCodeAt(i));
            }
    }


    One thing you don't say is what the source data is. You're casting to hex when you read, so I assume the data is decimal values

    Cheers,
    Menthos


  • MEW99

    The file I want to read is a torrent file. Its pure binary Data. Some strings but also things like Hashes and so on. All this is saved in a file. It is not a text file.

    I only want the pure hex data of the file itself.

    For example a file Contains "ABCD".
    The hex version of this file is "43444546" for example (I don't know if this is right).

  • daphneDolphin

    Err, hang on, what are you trying to read from that file then the actual torrent data, or just the header

    Do you know what the structure of the torrent data itself - I assume it's open source, although I wouldn't have thought it was ascii based.... of course I reserve the right to be proven wrong ;)



  • SuzukiBandit

    When I open the torrent in a text editor it looks like this:

    This is an Ubuntu Linux torrent (Yes.. not very good to post such things in a Microsoft forum, but I know how to get such a torrent fast and don't have to search a long time )

    d8:announce39:http://torrent.ubuntu.com:6969/announce7:comment29:Ubuntu CD releases.ubuntu.com13:creation datei1155127259e4:infod6:lengthi731670528e4:name33:ubuntu-6.06.1-alternate-amd64.iso12:piece lengthi524288e6:pieces27920: u}&n{“]! oc X #i £a-unU ow 1w“uL’) 7uz4 (*uif 3Vu1 'Ka!y “ o<A -`I^ (~ F O \| 3 qeA#;E _Y 3=÷±’ D uy o
    ·A”iA c >_4o [`(xgAu —xI I%f9OXKyuztCm +CU UR oL A
    .I ˉAO~pl· a' eY”QoIzo@oX}j ﹐+u c] Hc _u±· |81±a3PwyoN÷sqg±G0e*o oD °eUYVa4oRnEESa9N7UhwO.1A· 1’ d TiO UcAo3 E A Ar9”E5ˉ’r ma |e uEVU1xO”Fo Ko O$eT O~ iU o~בXen~
    I+u8x fkeA(
    R÷@IEN}e" AI b4 @a Qt ]K J.  T§X 'o 8U }}t aEZ
    ‥G?Nu)μ nF~oY MyeU&AN Oo U a5.{1m
    2 @ M‥A _$ UI eJoo o 5 ./°jR *c,&a; OIK TIAa OYo=A” NAN(+\ ¢i ENIy 9OBZ X*a |sPo~< u”u1UO¢UE,’a Q&'yp·°zb qC (o·c6nxiUIae`·uK u7Q}n.X Kcf LA UM1D, # yU~OipI¢,u‥Ae` z/JsyU Y) ,|V!o! 2…

    ... and so on. This is only a part of the file. The complete File would be too long.

    My problem is now that the charCodeAt() function looks in the ASCII Table. It's seems that the function doesn't properly work with the data posted above... Perhaps there are some characters in the file that are not printable. So you can't get the ASCII (or ANSI) code for that char. And I think that is the problem I have.

    Perhaps I should write an ActiveXObject that reads out the file. But I've never done that before. And only to read a file


  • Reading binary files