一、命令帮助
$ 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 capitalize variable names in C include file style (-i).
-c cols format <cols> octets per line. Default 16 (-i: 12, -ps: 30).
-E show characters in EBCDIC. Default ASCII.
-e little-endian dump (incompatible with -ps,-i,-r).
-g number of octets per group in normal output. Default 2 (-e: 4).
-h print this summary.
-i output in C include file style.
-l len stop after <len> octets.
-o off add <off> to the displayed file position.
-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".
测试文件内容
$ cat test.txt
abcdefghijklmnopqrstuvwxyz
二、文件转为16进制格式
xxd命令不带参数转换(每组2个字节,hexdump格式)
$ xxd test.txt
00000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop
00000010: 7172 7374 7576 7778 797a 0a qrstuvwxyz.
xxd命令不带参数转换(每组1个字节大写字母16进制,hexdump格式)
$ xxd -g 1 -u test.txt
00000000: 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 abcdefghijklmnop
00000010: 71 72 73 74 75 76 77 78 79 7A 0A qrstuvwxyz.
转为纯16进制
$ xxd -p test.txt
6162636465666768696a6b6c6d6e6f707172737475767778797a0a
转为C数组(带数组声明和长度变量定义)
$ xxd -i test.txt
unsigned char test_txt[] = {
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x0a
};
unsigned int test_txt_len = 27;
转为C数组(不带数组声明和长度变量定义)
$ xxd -i < test.txt
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x0a
三、16进制内容回转原文件
回转hexdump格式
$ cat test_hexdump.txt
00000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop
00000010: 7172 7374 7576 7778 797a 0a qrstuvwxyz.
$ xxd -r test_hexdump.txt
abcdefghijklmnopqrstuvwxyz
$ xxd -r test_hexdump.txt | xxd
00000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop
00000010: 7172 7374 7576 7778 797a 0a qrstuvwxyz.
回转纯16进制字符(可带空格)
$ cat test_hex.txt
61 62 63 64 65 66 67 68 69 6a 6 b6c6d6e6f 70 717 2737475767778797a0a
$ xxd -r -p test_hex.txt
abcdefghijklmnopqrstuvwxyz
$ xxd -r -p test_hex.txt | xxd
00000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop
00000010: 7172 7374 7576 7778 797a 0a qrstuvwxyz.
回转C数组(带数组声明和长度变量定义) 有问题(数据头尾多了0xed)
$ cat test.h
unsigned char test_txt[] = {
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x0a
};
unsigned int test_txt_len = 27;
$ xxd -r -p test.h
�abcdefghijklmnopqrstuvwxyz
$ xxd -r -p test.h | xxd
00000000: ed61 6263 6465 6667 6869 6a6b 6c6d 6e6f .abcdefghijklmno
00000010: 7071 7273 7475 7677 7879 7a0a ed pqrstuvwxyz..
回转C数组(不带数组声明和长度变量定义)
$ cat test_0xhex.txt
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x0a
$ xxd -r -p test_0xhex.txt
abcdefghijklmnopqrstuvwxyz
$ xxd -r -p test_0xhex.txt | xxd
00000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop
00000010: 7172 7374 7576 7778 797a 0a qrstuvwxyz.