如何在 Linux 中使用 xxd Hex Dumper 实用程序
在大多数类 Unix 操作系统上,xxd 命令帮助用户读取文件的十六进制值。还显示文件的 ASCII 表示形式,用户可以根据需要对其进行编辑。
该命令最初由 Juergen Weigert 于 1990 年开发,此后一直是基于 Linux 的系统的重要组成部分。
即使程序员也不会在位和字节级别上处理他们的代码——至少不是每天都这样。但是,如果需要,有一些简单的方法可以使用 xxd 十六进制转储程序来探索该级别的文件。
我们将在这篇文章中引导您使用六角转储器。
什么是六角转储器?
十六进制转储程序是一种以十六进制形式“转储”文件内容的程序。十六进制形式的数字有十六个字符。 A到F的字母代表数字10到15。
使用十六进制转储器比读取二进制文件容易得多。另外,该代码可以毫无问题地表示四位。编写十六进制也比编写二进制更容易,因为您不必编写一长串的零和一。
如果您曾经使用过 Photoshop 或任何类型的设计软件,您很可能使用十六进制数字来选择您想要使用的颜色。涉及的十六进制值以“#”字符开头。
xxd是什么?
xxd 是一个程序,它接收标准输入或文件作为输入,并返回带有 EBCDIC 或 ASCII 字符的十六进制转储。该程序还接受格式化的十六进制转储作为输入并将其转换为二进制。换句话说,您可以用它编辑或修补二进制文件。
该程序很少作为 Linux 发行版的一部分提供。然而,它可以在 Vim 编辑器中为需要它的人提供,并且几乎所有 Linux 发行版都附带它。
如果您的机器没有 Vim,您可以使用机器的包管理器安装它。使用 xxd 就像编写一样简单:
xxd [FILE]
该程序以列的形式打印出行号、人类可读的字符串和二进制内容(十六进制形式)。如果您有 ASCII 图表,您还可以在文本文件上使用该程序。
二进制文件也往往包含字符串,您可以使用文本编辑器研究这些字符串。大多数情况下,您使用 xxd 检查的文件是二进制的。
这些文件往往包含垃圾文本,但在文件的开头,您可能会找到文件类型及其创建方式等详细信息。
xxd 的语法
您可以使用参数或其相应选项来指定 xxd 程序的工作方式。
要查看命令手册或检查 xxd 的版本,您可以输入:
xxd [-h | -help] [-v | -version]
创建十六进制转储就像运行一样简单:
xxd [options] [infile [outfile]]
您还可以使用以下命令将十六进制转储转换为二进制:
xxd [-r | -revert] [options] [infile [outfile]]
下面是一个方便的表格,其中列出了您可以使用的所有参数、选项及其各自的功能:
Option | Parameter | Description |
a | -autoskip | A single asterisk character replaces any null lines in the file. |
b | -bits | It changes the output from a hex dump to bits. Using this option will result in outputs in octets, eight digits of zeroes and ones. Every line is preceded by a line number in hexadecimal form and an ASCII output after the number. You cannot use the -r, -i, and -p options if you use this option. |
c cols | -cols cols | It formats the output octets according to the number of <cols> you define. If you use no display switches, the format defaults to 16 octets. The option allows a maximum of 256 octets. If used in conjunction with the -ps option, the maximum limit doesn’t apply, and using “0” leads to a long, single-line output. |
C | Capitalize | Capitalizes variable names in C |
E | -EBCDIC | Alters character encoding in the right column from ASCII to EBCDIC. The hexadecimal values are unchanged. While it can be used with -r, -i, and -p, it has no effect in these combinations. |
e | This option instructs Vim to treat byte groups as words in little-endian byte order. So, in effect, the option changes the output to a little-endian hex dump. You can change the default 4-byte grouping with -g. Note that the option only changes the hex dump, and any ASCII/EBCDIC values remain unchanged. The -r, -i, and -p options won’t work with this option. | |
g bytes | -groupsize bytes | If you want to output to be grouped in sizes of <bytes>, use this option. If you specify 0 bytes, no grouping will be done. By default, the option is set to 2, but if you use bits mode, it’s 1 and 4 in little-endian mode. You cannot use grouping with postscript and include style. |
h | -help | Returns a summary of available commands. |
i | -include | Forces the output of the “C” option to return with file style information. |
l len | -len len | Stops the program after writing <len> objects. |
o offset | Puts an offset of value <offset> when displaying file position. | |
p | -postscript | Returns the output in plain hex dump style, also called the postscript continuous hex dump style. |
r | -revert | With this option, you can patch a hex dump into binary. If xxd isn’t writing to stdout, it will write into the output file without any truncation. You can use -r and -p to get plain hex dumps without line numbers and the column layout. Note that line breaks and whitespace is allowed anywhere in the output. |
seek offset | It is typically used after the -r option to add an offset to the hex dump. | |
s [+][-]seek | It starts the output after the number of bytes defined in <seek>. The plus sign indicates that the seek is relative to the current stdin file position, making it meaningless to use if xxd is not reading from stdin. The minus sign indicates that the seek has to be the specified number of characters from the end of the input. The two signs can be combined to indicate that the seek needs to begin before the current file position. Not using this option leads to xxd starting the output at the current file position. | |
u | Forces the program to use uppercase hex letters. | |
v | -version | Outputs the version string. |
xxd 退出状态
该程序使用五个返回码:
- Return Code
描述
- 0
没有错误
- -1
不支持的操作
- 1
选项错误
- 2
输入问题
- 3
输出问题
- 4,5
无法到达的寻找位置
结论
xxd 被认为是在 Linux 机器上处理不同类型文件的标准程序。通过本指南,您可以轻松地探索和更改这些文件。