十六进制转储转换字符串,格式字符串中包含转义字符 [英] Hexdump conversion string surrounded by escaped characters in format string

查看:143
本文介绍了十六进制转储转换字符串,格式字符串中包含转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get the following result from 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以及一些不需要的空格字符.

which is "\tt 78\t\tt 79\t\tt 7a\t" but I'm getting both the desired tabs and the literal letter t plus some unwanted space characters.

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

It works when using just a single trailing tab

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

给我

78  79  7a  

"78\t79\t7a\t",但不能用于单个前导标签

which is "78\t79\t7a\t" but not for a single leading tab

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

这给了我另一个错误

hexdump: %A: bad conversion character

我不确定该错误来自何处,因为任何地方都没有%A.

I'm not sure where that error is coming from since there is no %A anywhere.

根据手册页,\t应该是受支持的转义序列,我将其像printf中的其他任何字符一样对待.

According to the man page, \t should be a supported escape sequence and I'm treating it like any other character in printf.

此格式是必需的,并且必须用双引号(")引起来 分数.它被解释为fprintf样式的格式字符串(请参见 fprintf(3)),但以下情况除外:

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

推荐答案

此行为

This behavior is actually a fixed, not so long ago, bug. For affected versions there's a workaround: just put the leading backslash into a separate format string.

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

For example, the code you wanted would look like:

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

这篇关于十六进制转储转换字符串,格式字符串中包含转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆