xxd读取二进制文件

wgs2sgf需要处理二进制文件,以前一直在windows下用UltraEdit,无法复制文本,有点不方便。今天搜了一下linux下的类似工具,看到了xxd。在windows下可以通过msys使用xxd。下面就以msys中的xxd为例。

xxd的帮助信息:

$ xxd --help
Usage:
       xxd [options] [infile [outfile]]
    or
       xxd -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]]
Options:
    -a          toggle autoskip: A single '*' replaces nul-lines. Default off.
    -b          binary digit dump (incompatible with -ps,-i,-r). Default hex.
    -c cols     format <cols> octets per line. Default 16 (-i: 12, -ps: 30).
    -E          show characters in EBCDIC. Default ASCII.
    -g          number of octets per group in normal output. Default 2.
    -h          print this summary.
    -i          output in C include file style.
    -l len      stop after <len> octets.
    -ps         output in postscript plain hexdump style.
    -r          reverse operation: convert (or patch) hexdump into binary.
    -r -s off   revert with <off> added to file positions found in hexdump.
    -s [+][-]seek  start at <seek> bytes abs. (or +: rel.) infile offset.
    -u          use upper case hex letters.
    -v          show version: "xxd V1.10 27oct98 by Juergen Weigert".

使用示例

处理对象是wgs棋谱,棋谱信息头有122字节,由于wgs中有单字节表示的信息,希望1字节1组。执行以下命令:

$ xxd -g 1 -l 122 wgsfile.wgs
0000000: 02 00 00 00 31 59 2c 4f 00 00 c3 fb d7 d6 b6 bc  ....1Y,O........
0000010: b1 bb c6 f0 b9 e2 c1 cb 00 00 00 00 00 00 00 00  ................
0000020: 10 00 00 00 00 00 00 00 46 00 00 00 00 00 00 00  ........F.......
0000030: 00 00 00 00 2e 87 3b 2f 54 00 6a 6b 6c 00 00 00  ......;/T.jkl...
0000040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0000050: 10 00 00 00 4b ff ff ff 4d 00 00 00 00 00 00 00  ....K...M.......
0000060: 00 00 00 00 00 13 00 07 05 08 01 00 00 00 00 00  ................
0000070: 00 00 00 00 00 00 00 00 07 01                    ..........

和UltraEdit显示类似,只是中文字符没显示出来。不过已经解读过的信息就无所谓了。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注