在 Python 与 C 中打印空字符(“\x00") [英] Printing Null Character ("\x00") in Python vs C

查看:103
本文介绍了在 Python 与 C 中打印空字符(“\x00")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编写代码并运行语句时:

When I code and run the statement:

   print "\x00\x00\x00"

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

in Python it outputs three blank spaces followed by a newline. But in C, when I code and run the statement:

   char hex[] = "\x00\x00\x00";
   printf("%s\n", hex);

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

it interprets the NULL bytes like I thought it would: it doesn't do anything. So why in Python are NULL bytes treated as spaces?...

推荐答案

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

So why in Python are NULL bytes treated as spaces?

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

It's not. Your terminal/console is treating them like spaces. C just happens to stop at the first NUL, whereas Python outputs them. Try writing three NULs to stdout instead.

这篇关于在 Python 与 C 中打印空字符(“\x00")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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