ログからバイナリファイル変換
ログに出力した数字(10進数、16進数)をバイナリに変換するツール
バイナリに変換すれば、stiringなどのツールで各構造体のメンバにどんな
データが入っていたかを調べられたり、ascii以外の文字コードでもバイナリ
変換後にvim等のテキストエディタで読めるようになる。
使い方
log2bin.rb [-x] 出力ファイル
-x 入力が16進の場合
標準入力にバイナリ化したい文字列を渡す。
10進数は1~3桁、16進数は2桁の連続した数字を取り込み、それ以外
の文字列は無視。改行もOK
Ctrl-Dで入力終了
いつものように自己責任でお願いします。
log2bin.rb
#!/bin/env ruby
$HEX = false
if ARGV[0] == "-x"
$HEX = true
ARGV.shift
end
if ARGV.length != 1
puts "#{$0} outputfile"
exit -1
end
unless file = File.open(ARGV[0],"wb")
puts "#{$0} outputfile"
puts "#{ARGV[0]} can't write"
exit -1
end
begin
datas = []
if $HEX
STDIN.each do |line|
datas.concat(line.scan(/[\da-zA-Z]{2}/).map do|i| i.hex end)
end
else
STDIN.each do |line|
datas.concat(line.scan(/[\d]{1,3}/).map do|i| i.to_i end)
end
end
file.write(datas.pack("C*"))
ensure
file.close
end
$HEX = false
if ARGV[0] == "-x"
$HEX = true
ARGV.shift
end
if ARGV.length != 1
puts "#{$0} outputfile"
exit -1
end
unless file = File.open(ARGV[0],"wb")
puts "#{$0} outputfile"
puts "#{ARGV[0]} can't write"
exit -1
end
begin
datas = []
if $HEX
STDIN.each do |line|
datas.concat(line.scan(/[\da-zA-Z]{2}/).map do|i| i.hex end)
end
else
STDIN.each do |line|
datas.concat(line.scan(/[\d]{1,3}/).map do|i| i.to_i end)
end
end
file.write(datas.pack("C*"))
ensure
file.close
end
| 固定リンク
« undefined method use_transactional_fixtures= | トップページ | rails.vim pathには "app/models/blog.rb" というファイルがありません »
「ruby」カテゴリの記事
- windowsでgemで入れたコマンドがすべてin `load': no such file to load になる(2009.10.31)
- rake specが warning: useless use of == in void context(2009.06.19)
- WindowsでSelenium RC + Rspec(2009.06.06)
- ログからバイナリファイル変換(2009.05.17)
- rubyでscpによるリモートホストのファイルのバックアップ(2008.11.29)


コメント