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



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

    78      79      7a

"t78tt79tt7at"

尝试

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

导致错误:

hexdump: %  : bad conversion character

但是

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

正确产生

|78||79||7a|

添加空格:

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

做什么

    t 78        t 79        t 7a    

它是"tt 78ttt 79ttt 7at",但我得到了所需的标签文字字母t加上一些不需要的空格字符。

仅使用一个尾随标签时即可工作

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

给我

78  79  7a  

它是"78t79t7at",但不用于单个前导标签

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

这给了我另一个错误

hexdump: %A: bad conversion character

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

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

格式是必需的,必须用双引号(")括起来标记。它被解释为fprintf样式的格式字符串(请参阅fprintf(3)),但以下例外:

 +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                 
      <alert character>   a
      <backspace>         b
      <form-feed>         f
      <newline>           n
      <carriage return>   r
      <tab>               t
      <vertical tab>      v

这个行为实际上是不久前修复的bug。对于受影响的版本,有一个解决方法:只需将前导反斜杠放入一个单独的格式字符串中。

例如,您想要的代码看起来像:

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

最新更新