2011年10月08日


VBScript だけで16進数ダンプをコンソールに表示する



VBScript(WSH)は、Wscript で実行されると、標準出力を使え無いので
長いデータを表示する事にはむいていません。ですから、Cscript で実行
しなおす処理が必要になります
' ****************************************************
' 少なくともファイルを引数に一つ指定します
' ****************************************************
if Wscript.Arguments.Count = 0 then
	Wscript.Echo "ファイルを指定して下さい"
	Wscript.Quit
end if

Dim str : str = WScript.FullName
str = Right( str, 11 )
str = Ucase( str )

' ****************************************************
' Wscript で実行された場合は、Cscript で実行しなおし
' 但しその場合は一ページ表示毎に停止します
' ****************************************************
Set WshShell = CreateObject( "WScript.Shell" )
if str <> "CSCRIPT.EXE" then
	Call WshShell.Run( _
		"cmd.exe /c cscript.exe " & _
		"""" & WScript.ScriptFullName & _
		""" """ & Wscript.Arguments(0) & """ | more", _
		3 )
	WScript.Quit
end if

' ****************************************************
' ファイルを16進数でダンプします
' ****************************************************
Dim Fs,Stream
Dim InFile
Dim Kana
Dim KjFlg
Kana = Array( _
"。","「","」","、","・","ヲ","ァ","ィ","ゥ","ェ","ォ","ャ","ュ","ョ","ッ", _
"ー","ア","イ","ウ","エ","オ","カ","キ","ク","ケ","コ","サ","シ","ス","セ","ソ", _
"タ","チ","ツ","テ","ト","ナ","ニ","ヌ","ネ","ノ","ハ","ヒ","フ","ヘ","ホ","マ", _
"ミ","ム","メ","モ","ヤ","ユ","ヨ","ラ","リ","ル","レ","ロ","ワ","ン","゙","゚" )

Set Fs = CreateObject( "Scripting.FileSystemObject" )
Set Stream = CreateObject("ADODB.Stream")

InFile = WScript.Arguments(0)

Dim LineBuffer,DispBuffer,CWork,nCnt,strBuff,i,j

if not Fs.FileExists( InFile ) then
	Wscript.Echo "ファイルが存在しません"
	Wscript.Quit
end if

' ------------------------------------------------------
' Stream のオープン
Stream.Open
 
' ------------------------------------------------------
' Stream タイプの指定
Stream.Type = 1		' StreamTypeEnum の adTypeBinary
 
' ------------------------------------------------------
' 既存ファイルの内容を Stream に読み込む
Stream.LoadFromFile InFile
 
' ------------------------------------------------------
' バイナリ型の Stream オブジェクトからを読み取って加工
Bcnt = 0
nCnt = 0
KjFlg = ""

Do while not Stream.EOS

	if ( nCnt MOD 16 ) = 0 then
		Wscript.Echo "          0  1  2  3  4  5  6  7" _
		& "  8  9  A  B  C  D  E  F"
		Wscript.Echo "--------------------------------" _
		& "------------------------------------------"
	end if

	' 16 バイトの読込
	LineBuffer = Stream.Read(16)

	strBuff = ""
	For i = 1 to LenB( LineBuffer )
		CWork = MidB(LineBuffer,i,1)
		Cwork = AscB(Cwork)
		Cwork = Hex(Cwork)
		Cwork = Ucase(Cwork)
		Cwork = Right( "0" & Cwork, 2 )
		DispBuffer = DispBuffer & Cwork & " "
		strBuff = strBuff & CharConv( Cwork )
	Next

	Wscript.Echo _
		Right( _
			"00000000" & Ucase(Hex( nCnt * 16 )), 8 _
		) & " " & _
		Left(DispBuffer & String(49," "), 49 ) & strBuff
	DispBuffer = ""

	nCnt = nCnt + 1
 
Loop
 
' ------------------------------------------------------
' Stream を閉じる
Stream.Close

Set Stream = Nothing
Stream = Empty
Set Fs = Nothing
Fs = Empty

' ****************************************************
' 生データのテキスト
' ****************************************************
function CharConv( HexCode )

	Dim nCode

	nCode = Cint( "&H" & HexCode )

	if KjFlg = "" then
		if &H81 <= nCode and nCode <= &H84 or _
			&H88 <= nCode and nCode <= &H9f or _
			&HE0 <= nCode and nCode <= &HEA then
			KjFlg = HexCode
			CharConv = ""
			Exit Function
		end if
	else
		if HexCode <> "00" then
			KjFlg = KjFlg & HexCode
			CharConv = Chr( Cint( "&H" & KjFlg ) )
		else
			CharConv = ".."
		end if
		KjFlg = ""
		Exit Function
	end if

	if 0 <= nCode and nCode <= &H1F then
		CharConv = "."
	end if
	if &H20 <= nCode and nCode <= &H7E then
		CharConv = Chr(nCode)
	end if
	if &H7F <= nCode and nCode <= &HA0 then
		CharConv = "."
	end if
	if &HA1 <= nCode and nCode <= &HDF then
		CharConv = Kana(nCode-&HA1)
	end if
	if &HE0 <= nCode and nCode <= &HFF then
		CharConv = "."
	end if

end function



posted by at 15:52 | Comment(0) | ツール | このブログの読者になる | 更新情報をチェックする


この記事へのコメント
コメントを書く
お名前:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。