Char 和 printf 打印乱码
我是 C/C++ 的新手。我正在学习教程,但无法弄清楚我搞砸了什么。结果我总是胡言乱语。这是我的代码:
#include <stdio.h>
char code[] =
"\xb8\x8d\x5d\xfe\x19\xdb\xc9\xd9\x74\x24\xf4\x5b\x29\xc9\xb1"
"\x1c\x31\x43\x12\x03\x43\x12\x83\x4e\x59\x1c\xec\x8b\xa4\x39"
"\x7b\x0f\xdd\xe5\x3a\xfc\x64\x02\xcb\x33\x5e\x83\xde\x02\x17"
"\xfc\x63\xa3\xd3\xff\x14\x8c\x47\x12\xef\x12\x7c\xf6\xce\x81"
"\xec\xf2\x45\x2a\x76\xa0\xe3\x32\xa2\xf6\xee\x65\x5e\xa4\x7e"
"\xd6\x46\xa4\x1f\x62\xcd\x88\x35\x72\x0e\x6b\x7a\xdf\x08\xc9"
"\xab\x0f\xc6\x5a\xd6\xc9\xc5\xe8\x04\x17\x65\xa3\x47\x76\xac"
"\x6b\xa7\xce\x3d\x82\x18\x03\x20\x2a\x9b\x24\x13\xa1\x5a\x08"
"\x0d\x98\x1e\xa1\x74\xe9\xd4\xc0\xdc\x88\xda\xab\x74\xec\x63";
int main()
{
//int i;
//for (i=0;i<sizeof code; i++){
// code[i] = code[i] ^0xcc;
printf(code);
//}
}
我注释掉了这些位,因为我试图找出问题所在。最终我想把它们留在里面,看看会得到什么结果。但随着他们的进出,我就变得胡言乱语。
我确信我使用 printf 是错误的,但是我该如何使其工作呢?
谢谢。
I'm a newbie to C/C++. I'm working through a tutorial and can't figure out what I messed up. I keep getting jibberish as a result. Here's my code:
#include <stdio.h>
char code[] =
"\xb8\x8d\x5d\xfe\x19\xdb\xc9\xd9\x74\x24\xf4\x5b\x29\xc9\xb1"
"\x1c\x31\x43\x12\x03\x43\x12\x83\x4e\x59\x1c\xec\x8b\xa4\x39"
"\x7b\x0f\xdd\xe5\x3a\xfc\x64\x02\xcb\x33\x5e\x83\xde\x02\x17"
"\xfc\x63\xa3\xd3\xff\x14\x8c\x47\x12\xef\x12\x7c\xf6\xce\x81"
"\xec\xf2\x45\x2a\x76\xa0\xe3\x32\xa2\xf6\xee\x65\x5e\xa4\x7e"
"\xd6\x46\xa4\x1f\x62\xcd\x88\x35\x72\x0e\x6b\x7a\xdf\x08\xc9"
"\xab\x0f\xc6\x5a\xd6\xc9\xc5\xe8\x04\x17\x65\xa3\x47\x76\xac"
"\x6b\xa7\xce\x3d\x82\x18\x03\x20\x2a\x9b\x24\x13\xa1\x5a\x08"
"\x0d\x98\x1e\xa1\x74\xe9\xd4\xc0\xdc\x88\xda\xab\x74\xec\x63";
int main()
{
//int i;
//for (i=0;i<sizeof code; i++){
// code[i] = code[i] ^0xcc;
printf(code);
//}
}
I commented out those bits because I was trying to figure out where the problem is. Ultimately I want to leave them in and see what result I get. But with them in or out, I get jibberish.
I'm sure I'm using printf wrong, but how do I make this work?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试 printf("%s", code);
这可能会成功。
Try
printf("%s", code);
That might do the trick.
试试这个:
Try this:
\xb8 表示十六进制值为 0xB8 的字符... \x8d 表示十六进制值为 0x8D 的字符...没有人会坐下来为您解码整个字符串。看起来 printf 工作正常,但是垃圾进,垃圾出:)
实际上你让我很好奇,所以这是它在 Linux 控制台中的样子:
torp@torp:~/work/clocal$ ./乱码
��t��������t�c)ɱ1CC�NY실9{��:�d�3^���c����G�|���E*v��2��e ^�~�F�b5rkzɫ�Z����e�Gv�k��=�▒ *�$�Z
\xb8 means a char with value 0xB8 hex... \x8d means the char with value 0x8D hex... no one's going to sit around and decode the entire string for you. It looks like printf is working perfectly but garbage in, garbage out :)
Actually you made me curious so here's how it looks in a linux console:
torp@torp:~/work/clocal$ ./gibberish
��t���܈ګt�c)ɱ1CC�NY실9{��:�d�3^���c����G�|���E*v��2���e^�~�F�b5rkzɫ�Z����e�Gv�k��=�▒ *�$�Z
据我所知,
code
的内容是乱码。它不是 ASCII、UTF-8、Latin-1 或 EBCDIC。当将其写入文件并对其运行file
命令时,它只会显示“data”。当我恢复注释掉的代码(用0xcc
异或每个字节)时,它仍然是乱码。因此,当你的程序打印出乱码时,它正在做它应该做的事情。
你用的是什么教程?它告诉你要期待什么?
As far as I can tell, the contents of
code
are gibberish. It's not ASCII, UTF-8, Latin-1, or EBCDIC. When write it to a file and run thefile
command on it, it just says "data". And when I restore the commented-out code, which xors each byte with0xcc
, it's still gibberish.So when your program prints gibberish, it's doing exactly what it should.
What tutorial are you using? What did it tell you to expect?
尝试使用“%x”或“X”格式说明符:
Try using the "%x" or "X" format specifiers: