Using VBScript this can all be done using MS Objects:
To cache the binary file, use the Adodb.Stream (v 2.5+)
To Base64 encode, use the Capicom.Utilities (v 2.0+)
Searching groups.google should turn up plenty of example code.
Back to the Oscars,
Mark Pryor
>I have been trying to track down an example of using VBS to open a
>binary file.
The FileSystemObject's Read method returns a string, but it does not
modify the data in any way, so you can easily convert the string to a
numerical format using the Asc() function on each character in the
string:
Set fso = CreateObject("Scripting.FileSystemObject")
set ts = fso.OpenTextFile("C:\winnt\explorer.exe")
Wscript.Echo "The first byte is : 0x" & Hex(Asc(ts.Read(1)))
ts.Close
I used this to write my own hex dump tool. It must be run with
cscript.exe unless you really like messageboxes (the regular
expression stuff is just to filter out nonprintable characters and is
probably far from complete):
Option Explicit
Dim fso, ts, count, TextLine, HexText
Dim re
Set re = new RegExp
Set fso = CreateObject("Scripting.FileSystemObject")
If Wscript.Arguments.Count <> 1 Then
wscript.echo "One parameter, please"
wscript.quit
End If
Set ts = fso.OpenTextFile(Wscript.Arguments(0), 1, False)
re.pattern = _
"[^A-Za-z0-9_\(\)\!\x22#、%&=\\\*',;:\?\/\$」\-\+\{\[\]\}>< ]"
re.Global = True
Do While Not ts.AtEndofStream
TextLine = ts.read(16)
HexText = ""
For count = 1 To Len(TextLine)
HexText = HexText & _
Right(0 & Hex(Asc(Mid(TextLine,Count,1))),2) & ":"
Next
TextLine = re.Replace (TextLine, ".")
HexText = Mid(HexText,1,Len(HexText)-1)
wscript.echo TextLine & Space(20 - Len(TextLine)) & HexText
Loop
ts.close
--
Helge Wunderlich
(Please remove the spam trap if you wish to send email)
Thanks for your reply.
Just a few questions though. Can the FileSystemObject be then used to
write the hex data obtained form the above code?
Also I tried the aove code and it returned 4D as the first byte. I
opened explorer.exe with my hex editor and didnt even find 4D in the
whole file.
Could you please explain why this is.
I'm a big programing noob.
TIA for your help.
--
--
RoscoeTruthless <roscoet...@lycos.com> wrote in message
news:oawfa.34462$cB3.1...@nnrp1.uunet.ca...
Yes. All you need to do, is to convert the numerical byte value back
to a character using the Chr() function. For example, if you want to
write a zero, you use:
ts.Write Chr(0)
If your numbers are represented by a hex value in a string, you must,
of course, convert them to a numerical value first.
>Also I tried the aove code and it returned 4D as the first byte.
That's the correct result. All EXE files start with an uppercase "M",
which has the ASCII value 4D hex or 77 decimal.
>I
>opened explorer.exe with my hex editor and didnt even find 4D in the
>whole file.
I cannot explain it. I just looked at my Explorer.exe (I have Windows
2000 SP3). It contains several 4Ds. It even contains one instance of
the ASCII sequence "4D", so no matter how I search (text or hex), I
still find at least one.
There should not be any need to search. Simply looking at the very
first byte should be all you need. Which hex editor do you use ?
Something called binEdit.
Doesn't make any sense.
>Something called binEdit.
I found a freeware program by that name, written by Bob Peiffer. Is
that it ?
>Doesn't make any sense.
It really doesn't. I downloaded BinEdit and opened Explorer.exe with
it. Here's a screen capture from the result:
http://home.no.net/helgeww/temp/binedit.gif
As you can see, the first byte is definitely 4d.
>>Doesn't make any sense.
> It really doesn't. I downloaded BinEdit and opened Explorer.exe with
> it. Here's a screen capture from the result:
> As you can see, the first byte is definitely 4d.
You said that all exe files start with M (0x4D).
I have opend some otehrs and they all seem to start with M. Strange why
my exploer.exe file is different.
Mystery abounds.
But the sample script I showed proves that the first byte is indeed
4d.
What does your Explorer.exe look like, then ?