输出乱码有关问题
输出乱码问题
这段代码最后的输出是乱码,不知道问题在哪?请大家帮忙指正,多谢了!!
C/C++ code
#include <stdio.h> #include <string.h> int main() { int i,j; char buf[] = "123456781212121212121212121212121212121212"; char tmp_buf[512]; int len = strlen(buf); for (i = 0,j = 0; i < len && j < 512; i++) { if (i && ((i % 8) == 0)) tmp_buf[j++] = '\n'; sscanf(&buf[i],"%x",&tmp_buf[j++]); tmp_buf[j++] = ' '; } tmp_buf[j] = '\0'; printf("%s\n",tmp_buf); return 0; }
Answers
你这段代码问题很多
[code=C/C++][/code]
#include <stdio.h>
#include <string.h>
int main()
{
int i,j;
char buf[] = "123456781212121212121212121212121212121212";
char tmpbuf[512];
int len = strlen(buf);
for (i = 0,j = 0; i < len && j < 512; i++) {
if (i && ((i % 8) == 0))
tmpbuf[j++] = '\n';
sscanf(&buf[i],"%x",&tmpbuf[j++]);//你这样存入tmpbuf[j++]中的是int型的数值
tmpbuf[j++] = ' ';
}
tmpbuf[j] = '\0';
printf("%s\n",tmp_buf);//这个时候输出的是char型的输出的是123456781212121212121212121212121212121212对应的是ASC
return 0;
}