在Python与C中打印空字符("\ x00")

Bhu*_*bus 2 c python null string-formatting

当我编写并运行语句时:

   print "\x00\x00\x00"
Run Code Online (Sandbox Code Playgroud)

在Python中,它输出三个空格,后跟换行符.但是在C中,当我编写并运行语句时:

   char hex[] = "\x00\x00\x00";
   printf("%s\n", hex);
Run Code Online (Sandbox Code Playgroud)

它像我想的那样解释NULL字节:它没有做任何事情.那么为什么在Python中将NULL字节视为空格?...

Ign*_*ams 9

那么为什么在Python中将NULL字节视为空格?

不是.您的终端/控制台将它们视为空格.C恰好在第一个NUL停止,而Python输出它们.尝试将三个NUL写入stdout.