printf 与 %s 以包含空字符 [英] printf with %s to include null characters
问题描述
我需要连接一些字符串,并且需要包含 NULL 字节.我不想将 '\0' 视为终止字节.我想保存我宝贵的 NULL 字节!
I need to concatenate some strings, and I need to include NULL bytes. I don't want to treat a '\0' as a terminating byte. I want to save my valuable NULL bytes!
在代码示例中,如果
char *a = "\0hey\0\0";
我需要以输出\0hey\0\0"的格式打印.
I need to printf in a format that will output "\0hey\0\0".
-AUstin
推荐答案
怎么样:
int i;
for(i = 0; i < 4; i++)
printf("%c", a[i]);
如果您希望在格式字符串中指定 %s
时使用 'printf-like' 函数,您可以将上述代码包含在您自己的函数中.但正如@Neil 所提到的,您将很难找到替代查找空字节来确定字符串长度的方法.为此,我想您可以使用某种转义字符.
If you want a 'printf-like' function to use this when you specify %s
in a format string you could include the above code in your own function. But as @Neil mentioned, you'll struggle finding an alternative to looking for null bytes to determine the length of strings. For that I guess you could use some kind of escape character.
这篇关于printf 与 %s 以包含空字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!