C语言 C - %x 格式说明符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15108932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C - The %x format specifier
提问by Matthew
I have a small question. I know that the %x format specifier can be used to read values from the stack in a format string attack.
我有一个小问题。我知道 %x 格式说明符可用于在格式字符串攻击中从堆栈中读取值。
I found the following code:
我找到了以下代码:
%08x%08x%08x%08x
What does the 08 mean? What is it doing exactly? Thanks :)
08是什么意思?它究竟在做什么?谢谢 :)
回答by sonicwave
Break-down:
分解:
8says that you want to show 8 digits0that you want to prefix with0's instead of just blank spacesxthat you want to print in lower-case hexadecimal.
8说你想显示 8 位数字0你想用0's 而不是空格作为前缀x要以小写十六进制打印。
Quick example (thanks to Grijesh Chauhan):
快速示例(感谢 Grijesh Chauhan):
#include <stdio.h>
int main() {
int data = 29;
printf("%x\n", data); // just print data
printf("%0x\n", data); // just print data ('0' on its own has no effect)
printf("%8x\n", data); // print in 8 width and pad with blank spaces
printf("%08x\n", data); // print in 8 width and pad with 0's
return 0;
}
Output:
输出:
1d
1d
1d
0000001d
Also see http://www.cplusplus.com/reference/cstdio/printf/for reference.
回答by Vladimir
%08xmeans that every number should be printed at least 8 characters wide with filling all missing digits with zeros, e.g. for '1' output will be 00000001
%08x意味着每个数字都应该打印至少 8 个字符宽,并用零填充所有丢失的数字,例如对于 '1' 输出将是 00000001
回答by Tony The Lion
That specifies the how many digits you want it to show.
这指定了您希望它显示的数字数量。
integer value or * that specifies minimum field width. The result is padded with space characters (by default), if required, on the left when right-justified, or on the right if left-justified. In the case when * is used, the width is specified by an additional argument of type int. If the value of the argument is negative, it results with the - flag specified and positive field width.
指定最小字段宽度的整数值或 *。结果用空格字符填充(默认情况下),如果需要,右对齐时在左侧,如果左对齐则在右侧。在使用 * 的情况下,宽度由 int 类型的附加参数指定。如果参数的值为负,则结果为 - 指定的标志和正的字段宽度。
回答by David Ranieri
From http://en.wikipedia.org/wiki/Printf_format_string
来自http://en.wikipedia.org/wiki/Printf_format_string
use 0 instead of spaces to pad a field when the width option is specified. For example, printf("%2d", 3)results in " 3", while printf("%02d", 3)results in "03".
当指定宽度选项时,使用 0 而不是空格来填充字段。例如,printf("%2d", 3)结果为“3”,而printf("%02d", 3)结果为“03”。
回答by IamTheWalrus
The format string attack on printf you mentioned isn't specific to the "%x" formatting - in any case where printf has more formatting parameters than passed variables, it will read values from the stack that do not belong to it. You will get the same issue with %d for example. %x is useful when you want to see those values as hex.
您提到的对 printf 的格式字符串攻击并非特定于“%x”格式 - 在 printf 具有比传递的变量更多的格式参数的任何情况下,它将从堆栈中读取不属于它的值。例如,您将遇到与 %d 相同的问题。当您想以十六进制形式查看这些值时,%x 很有用。
As explained in previous answers, %08x will produce a 8 digits hex number, padded by preceding zeros.
如之前的答案中所述, %08x 将产生一个 8 位十六进制数字,由前面的零填充。
Using the formatting in your code example in printf, with no additional parameters:
在 printf 中使用代码示例中的格式,没有附加参数:
printf ("%08x %08x %08x %08x");
Will fetch 4 parameters from the stack and display them as 8-digits padded hex numbers.
将从堆栈中获取 4 个参数并将它们显示为 8 位填充的十六进制数字。

