Hi,
I want to know if there's any method to open a file in Hex with
VBScript
Thx
Omar Abid

Re: Hex program by noone

noone
Thu Jun 14 16:51:33 CDT 2007

Il giorno Tue, 12 Jun 2007 04:26:50 -0700, Omar Abid <omar.abid2006@gmail.com> ha scritto:
>I want to know if there's any method to open a file in Hex with
>VBScript

This reads a single char:
set ts=f.OpenAsTextStream(ForReading,0)
ch=ts.read(1)

This shows the hex dump in an IE window.

'************************************************
' File: HexDump2.vbs (VBScript)
' Author: Giovanni Cenati
' 16 june 2005
' Mostra in IE un file in esadecimale
' Shows a hex dump of a selected file
' http://digilander.libero.it/Cenati
' Codice utilizzabile citando il sito.
' Based on a script post by spmlinton at sk sympatico ca
' in the newsgroup microsoft.public.scripting.vbscript
' on Thu, 27 Mar 2003
'************************************************
Set objArgs = WScript.Arguments 'Vedo se ci sono degli argomenti passati allo script
if objargs.count=0 then
Set comDlg=CreateObject("MSComDlg.CommonDialog.1")
const cdlOFNHideReadOnly = 4 'Nasconde la casella "sola lettura"
const cdlOFNFileMustExist = 4096 'Puoi scegliere solo files esistenti
Const cdlOFNExplorer = 524288 'Finestra in stile win95
comDlg.Flags = cdlOFNHideReadOnly + cdlOFNFileMustExist + cdlOFNExplorer
comDlg.Filter = "Tutti i files|*.*"
comDlg.DialogTitle="Visualizza un file in formato esadecimale"
'comDlg.InitDir="c:\"
comDlg.maxfilesize=500
'comDlg.FileName="c:\autoexec.bat"
comDlg.ShowOpen
if comDlg.FileName="" then wscript.quit
FName= comDlg.FileName
'Ricordo il filename scelto. Saves the filename in variable
set comDlg=nothing 'Scarica l'oggetto. Discards object
else
fName = objArgs(0)
end if

' Lancia Internet Explorer per mostrare i risultati.
Set oIE = WScript.CreateObject("InternetExplorer.Application")
oIE.navigate "about:blank" ' documento vuoto. Empty doc.
' Importante: aspetta che Internet Explorer sia pronto
Do While (oIE.Busy) ' Waits until IE is ready
WScript.Sleep 200 ' Sospende per 200 millisecondi.
Loop
oIE.Visible = 1 ' Internet Explorer è visibile.
oIE.ToolBar = 1 ' ed ha la toolbar così è presente il pulsante "stampa"
Set doc1 = oIE.Document ' Punta al documento...
doc1.open ' ...lo apre
' Scrive le intestazioni html. Writes html headers
doc1.writeln("<html><head><title>" & Title & "</title></head>")
doc1.writeln("<body bgcolor='#FAF0F0'><pre>")

Const ForReading = 1
set fso=createobject("scripting.filesystemobject")
set f=fso.getfile(FName) 'apro il file. Open file for reading
set ts=f.OpenAsTextStream(ForReading,0) 'in lettura
byteCtr=0
chCtr=0

doc1.writeln(Fname)
doc1.writeln(f.size & " bytes")
doc1.writeln()
do while not ts.atendofstream
ch=ts.read(1) 'Legge un carattere. Reads a char
chCtr=chCtr + 1 'E ne tiene nota nel conteggio. Counts it

hexch=hex(asc(ch)) 'Ne determina il codice ascii. Finds ascii code
if len(hexch)< 2 then hexch="0" & hexch
hexLine=hexLine & hexch & " " 'Adds it to the string

if asc(ch)> 31 and asc(ch)< 127 then 'Printable?
if ch=">" then ch="&gt;" '(IE lo interpreterebbe come un tag)
if ch="<" then ch="&lt;" '(IE would think it's a tag)
ascStr=ascStr & ch 'è stampabile. Yes
else
ascStr=ascStr & "." 'non è stampabile. No
end if



'Prepara la stampa di una riga:
'indirizzo - 16 esadecimali - 16 caratteri ascii
'Prepares a line to be displayed:
'Address, 16 hex characters, 16 ascii
if chCtr=16 or ts.atendofstream then
lineStr=byteCtr & vbtab & hexLine & vbtab & ascStr
'Now manages last line if it has less than 16 chars
if chCtr<16 then 'Se l'ultima riga ha meno di 16 cartteri...
lineStr=byteCtr & vbtab & hexLine 'inizio normalmente
for r= (chCtr+1) to 16 'poi riempio la parte restante della riga
lineStr= lineStr & "-- " 'con dei caratteri
next
lineStr = lineStr & vbtab & ascStr 'e finisco normalmente
end if
'Writes output to Internet Explorer
doc1.writeln(lineStr) 'Scrive in IE la riga creata
byteCtr=byteCtr + chCtr 'incrementa l'indirizzo
chCtr=0 'e azzera il parziale
ascStr=""
hexLine=""
end if
loop

'Chiude le intestazioni html
'Closes html headers
doc1.writeln("</pre></body></html>")
doc1.close

set fso=nothing
set f=nothing
set ts= nothing


--
Giovanni Cenati (Aosta, Italy)
Write to user "Reventlov" and domain at katamail com
http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
--