【问题标题】:Hexdump conversion string surrounded by escaped characters in format string格式字符串中被转义字符包围的 Hexdump 转换字符串
【发布时间】:2015-12-07 23:18:51
【问题描述】:

我正在尝试从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|

添加空格:

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加上一些不需要的空格字符。

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

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 中的任何其他字符。

格式为必填项,必须用双引号(“”)括起来 分数。它被解释为 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                 \0
      <alert character>   \a
      <backspace>         \b
      <form-feed>         \f
      <newline>           \n
      <carriage return>   \r
      <tab>               \t
      <vertical tab>      \v

【问题讨论】:

  • 你的 shell 是否有可能首先尝试扩展单个反斜杠?即,尝试使用双反斜杠。
  • 前导\\t 的错误相同。使用 bash 并且整个 arg 位于单引号字符串中,因此它不应该在 shell 端进行任何扩展。
  • hexdump: %\: bad conversion character 同时使用\\t%x\\t

标签: shell command-line hexdump


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2019-04-01
    • 1970-01-01
    • 2019-09-19
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多