[RUBY] HexDump Ruby Code(루비로 헥스 정보 출력하기)
실행 인자값으로 받을 값(파일명) 을 hexdump라는 함수를 통해 hex값으로 출력하는 코드입니다.
이 코드의 핵심부분인 hexdump 함수에 대해 보겠습니다. filename 파라미터를 통해 받은 인자값을 File.open 으로 로드한 후 each_byte 를 통해 c에 1byte씩 저장한 후 출력하는 간단한 코드입니다.
1 def hexdump(filename, start = 0, finish = nil, width = 16) 2 ascii = ‘’ 3 counter = 0 4 print ‘%06x ‘ % start 5 File.open(filename).each_byte do |c| 6 if counter >= start 7 print ‘%02x ‘ % c 8 ascii « (c.between?(32, 126) ? c : ?.) 9 if ascii.length >= width 10 puts ascii 11 ascii = ‘’ 12 print ‘%06x ‘ % (counter + 1) 13 end 14 end 15 throw :done if finish && finish <= counter 16 counter += 1 17 end rescue :done 18 puts ‘ ‘ * (width - ascii.length) + ascii 19 end 20 21 22 if $0 == FILE 23 if ARGV.empty? 24 hexdump $0 25 else 26 filename = ARGV.shift 27 hexdump filename, *(ARGV.map {|arg| arg.to_i }) 28 end 29 end 30 31
code def hexdump(filename, start = 0, finish = nil, width = 16) ascii = ‘’ counter = 0 print ‘%06x ‘ % start File.open(filename).each_byte do |c| if counter >= start print ‘%02x ‘ % c ascii « (c.between?(32, 126) ? c : ?.) if ascii.length >= width puts ascii ascii = ‘’ print ‘%06x ‘ % (counter + 1) end end throw :done if finish && finish <= counter counter += 1 end rescue :done puts ‘ ‘ * (width - ascii.length) + ascii end
if $0 == FILE if ARGV.empty? puts “Usage: “+$0+” [FILE]” puts “Ex) “+$0+”test.jpg” else filename = ARGV.shift hexdump filename, *(ARGV.map {|arg| arg.to_i }) end end
실행하면 아래와 같이 hexdata 가 나타남을 볼 수 있습니다.
ruby hexdump.rb
Usage: hexdump.rb [FILE] Ex) hexdump.rbtest.jpg
ruby hexdump.rb 123.jpg
000000 ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 01 ……JFIF…… 000010 00 01 00 00 ff e1 07 6e 45 78 69 66 00 00 4d 4d …….nExif..MM 000020 00 2a 00 00 00 08 00 06 01 12 00 03 00 00 00 01 .*………….. 000030 00 01 00 00 01 1a 00 05 00 00 00 01 00 00 00 56 ……………V 000040 01 1b 00 05 00 00 00 01 00 00 00 5e 01 28 00 03 ………..^.(.. 000050 00 00 00 01 00 02 00 00 02 13 00 03 00 00 00 01 ……………. 000060 ff ff 00 00 87 69 00 04 00 00 00 01 00 00 00 66 …..i………f 000070 00 00 00 7a 00 00 00 01 00 00 00 01 00 00 00 01 …z………… 000080 00 00 00 01 00 01 90 00 00 02 00 00 00 00 00 00 ……………. 000090 00 00 00 00 00 00 00 00 00 0f 00 fe 00 04 00 00 ……………. 0000a0 00 01 00 00 00 00 01 00 00 04 00 00 00 01 00 00 ……………. 0000b0 00 a0 01 01 00 04 00 00 00 01 00 00 00 64 01 02 ………….d.. 0000c0 00 03 00 00 00 03 00 00 01 34 01 03 00 03 00 00 ………4…… 0000d0 00 01 00 06 00 00 01 06 00 03 00 00 00 01 00 06 ……………. 0000e0 00 00 01 15 00 03 00 00 00 01 00 03 00 00 01 16 ……………. 0000f0 00 04 00 00 00 01 00 00 00 64 01 1a 00 05 00 00 ………d…… 000100 00 01 00 00 01 3a 01 1b 00 05 00 00 00 01 00 00 …..:………. 000110 01 42 01 28 00 03 00 00 00 01 00 02 00 00 02 00 .B.(………… 000120 00 03 00 00 00 01 00 01 00 00 02 01 00 04 00 00 ……………. 000130 00 01 00 00 01 4a 02 02 00 04 00 00 00 01 00 00 …..J………. 000140 06 1b 02 12 00 03 00 00 00 02 00 02 00 02 00 00 ……………. 000150 00 00 00 08 00 08 00 08 00 00 01 2c 00 00 00 01 ………..,…. 000160 00 00 01 2c 00 00 00 01 ff d8 ff db 00 43 00 08 …,………C.. 000170 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d ……………. 000180 0c 0b 0b 0c 19 12 13 0f 14 1d 1a 1f 1e 1d 1a 1c ……………. 000190 1c 20 24 2e 27 20 22 2c 23 1c 1c 28 37 29 2c 30 . $.’ “,#..(7),0 0001a0 31 34 34 34 1f 27 39 3d 38 32 3c 2e 33 34 32 ff 1444.’9=82<.342. 0001b0 db 00 43 01 09 09 09 0c 0b 0c 18 0d 0d 18 32 21 ..C………..2! 0001c0 1c 21 32 32 32 32 32 32 32 32 32 32 32 32 32 32 .!22222222222222
Reference
http://c2.com/cgi/wiki?HexDumpInManyProgrammingLanguages