バイナリファイルの内容表示する

display_binary_file.rb

binary_file = 'test.aco'
decimal_mode = false
#decimal_mode = true
item_num = 0

open(binary_file, 'rb') {|f|
  until f.eof?
    if decimal_mode
      print "%5d" % (f.getc * 256 + f.getc)
    else
      print "%02x%02x" % [f.getc, f.getc]
    end
    item_num += 1
    
    item_num % 8 == 0 ? puts : print(" ")
  end
}

実行結果

> ruby -w display_binary_file.rb
0001 0021 0000 de00 df00 de00 0000 0000
c600 c700 c600 0000 0000 9c00 a200 a500
0000 0000 6300 6900 6b00 0000 0000 3100
3400 3100 0000 0000 2100 2400 2100 0000
0000 ff00 cf00 ce00 0000 0000 f700 aa00
ad00 0000 0000 e700 4d00 5200 0000 0000
de00 3000 3900 0000 0000 b500 8a00 7300
0000 0000 9c00 6100 4200 0000 0000 7300
2800 0000 0000 0000 4a00 2000 0800 0000
0000 ff00 df00 d600 0000 0000 ff00 d300
c600 0000 0000 ff00 c300 ad00 0000 0000
ff00 b200 9c00 0000 0000 ff00 ef00 9400
0000 0000 ff00 df00 4a00 0000 0000 f700
b200 8c00 0000 0000 ef00 8200 4200 0000
0000 de00 7d00 4a00 0000 0000 de00 f300
ef00 0000 0000 ad00 db00 ce00 0000 0000
7b00 be00 ad00 0000 0000 3100 6d00 6300
0000 0000 2900 5d00 5200 0000 0000 d600
eb00 ff00 0000 0000 8c00 cb00 ef00 0000
0000 7b00 9600 ce00 0000 0000 b500 ae00
d600 0000 0000 7b00 7500 b500 0000 

hexl-mode

emacs にはバイナリファイルを編集するためのモードとして
「hexl-mode」が標準で備わっているのでそっち使った方が早かった。
.aco 開くたびに「M-x hexl-mode」してもいいんだけど
関連付けしておいた方が便利なのでしておいた。(何度も M-x hexl-mode したのちに。)

(add-to-list 'auto-mode-alist '("\\.aco$" . hexl-mode))
hexl-mode 関連のコマンドとか
M-x hexl-mode 現在開いているバッファを hexl-mode にする。
M-x hexl-find-file 指定したファイルを hexl-mode で開く。
M-x hexl-mode-exit hexl-mode から抜ける。
C-M-x 16進数を入力 (hexl-mode のとき)

.aco について

「.aco」は Adobe Color ファイルの拡張子。
Photoshop でカラーパレットのデータを読み書きするときに使われている。
SAI でも SST を通して使える。