Hexdump 转换字符串被格式字符串中的转义字符包围

Hexdump conversion string surrounded by escaped characters in format string

我正在尝试从 hexdump 获得以下结果:

    78      79      7a

"\t78\t\t79\t\t7a\t"

正在尝试

echo -n xyz | hexdump -e '1/1 "\t%x\t"'

结果出错:

hexdump: %  : bad conversion character

但是

echo -n xyz | hexdump -e '1/1 "|%x|"'

正确产生

|78||79||7a|

添加spaces:

echo -n xyz | hexdump -e '1/1 "\t %x \t"'

某事:

    t 78        t 79        t 7a    

这是 "\tt 78\t\tt 79\t\tt 7a\t" 但我得到了所需的选项卡 文字字母 t 加上一些不需要的 space 字符。

仅使用一个尾随制表符时有效

echo -n xyz | hexdump -e '1/1 "%x\t"'

给我

78  79  7a  

这是 "78\t79\t7a\t" 但不是单个前导标签

echo -n xyz | hexdump -e '1/1 "\t%x"'

这给了我另一个错误

hexdump: %A: bad conversion character

我不确定这个错误是从哪里来的,因为任何地方都没有 %A

根据手册页,\t 应该是受支持的转义序列,我将其视为 printf 中的任何其他字符。

The format is required and must be surrounded by double quote (" ") marks. It is interpreted as a fprintf-style format string (see fprintf(3)), with the following exceptions:

 +o   An asterisk (*) may not be used as a field width    or precision.

 +o   A byte count or field precision is required for each ``s'' con-
     version character (unlike the fprintf(3) default which prints
     the entire string if the precision is unspecified).

 +o   The conversion characters ``h'',    ``l'', ``n'', ``p'' and ``q''
     are not supported.

 +o   The single character escape sequences described in the C    stan-
     dard are supported:

      NUL                 [=21=]
      <alert character>   \a
      <backspace>         \b
      <form-feed>         \f
      <newline>           \n
      <carriage return>   \r
      <tab>               \t
      <vertical tab>      \v

这种行为is actually a fixed, not so long ago, bug。对于受影响的版本,有一个解决方法:只需将前导反斜杠放入单独的格式字符串中。

例如,您想要的代码如下所示:

echo -n xyz | hexdump -e '"\t" 1/1 "%x"'