标题: 单片机C语言printf中文字符串乱码 求助 [打印本页]

作者: MYJ918    时间: 2022-5-17 16:00
标题: 单片机C语言printf中文字符串乱码 求助
代码部分
while(1)
        {
        printf("系统初始化正常 hello word : %u\r\n",Cnt++);


串口输出部分
系统初始化粘?hello word : 1
系统初始化粘?hello word : 2
系统初始化粘?hello word : 3
系统初始化粘?hello word : 4
系统初始化粘?hello word : 5
系统初始化粘?hello word : 6
系统初始化粘?hello word : 7
?低吵跏蓟常 hello word : 8
系?吵跏蓟常 hello word : 9
系统初始化粘?hello word : 10
系统初始化粘?hello word : 11
系统初?蓟常 hello word : 12
系统初始化粘?hello word : 13
系统初始?常 hello word : 14
系统初始化粘?hello word : 15
系统初始化?常 hello word : 16
系统初始化粘?hello word : 17
系统初始化粘? hello word : 18
系统初始化粘?hello word : 19
系统初始化粘?hello word : 20
系统初始化粘?hello word : 21
系统初始化粘?hello word : 22
系统初始化粘?hello word : 23
系统初始化粘?hello word : 24
系统初始化粘?hello word : 25
系统初始化粘?hello word : 26
系统初始化粘?hello word : 27
系统初始化粘?hello word : 28
系统初始化粘?hello word : 29
系统初始化粘?hello word : 30
系统初始化粘?hello word : 31
系统初始化粘?hello word : 32
系统初始化粘?hello word : 33
系统初始化粘?hello word : 34
系统初始化粘?hello word : 35
系统初始化粘?hello word : 36
系统初始化粘?hello word : 37
?低吵跏蓟常 hello word : 38
系统初始化粘?hello word : 39
系统初始化粘?hello word : 40
系统初始化粘?hello word : 41
系统初始化粘?hello word : 42
系统初始化粘?hello word : 43
系统初始化粘?hello word : 44
系统初始化粘?hello word : 45
系统初始?常 hello word : 46
系统初始化粘?hello word : 47
系统初始化?常 hello word : 48
系统初始化粘?hello word : 49
系统初始化粘? hello word : 50
系统初始化粘?hello word : 51
系统初始化粘?hello word : 52
系统初始化粘?hello word : 53
系统初始化粘?hello word : 54
系统初始化粘?hello word : 55
系统初始化粘?hello word : 56
系统初始化粘?hello word : 57
系统初始化粘?hello word : 58
系统初始化粘?hello word : 59
系统初始化粘?hello word : 60
系统初始化粘?hello word : 61
系统初始化粘?hello word : 62
系统初始化粘?hello word : 63
系统初始化粘?hello word : 64
系统初始化粘?hello word : 65
系统初始化粘?hello word : 66
系统初始化粘?hello word : 67
系统初始化粘?hello word : 68
系统初始化粘?hello word : 69
?低吵跏蓟常 hello word : 70
系统初始化粘?hello word : 71
系?吵跏蓟常 hello word : 72
系统初始化粘?hello word : 73
系统初始化粘?hello word : 74
系统初始化粘?hello word : 75
系统初始化粘?hello word : 76
系统初始化粘?hello word : 77
系统初始?常 hello word : 78
系统初始化粘?hello word : 79
系统初始化?常 hello word : 80
系统初始化粘?hello word : 81
系统初始化粘? hello word : 82
系统初始化粘?hello word : 83
系统初始化粘?hello word : 84
系统初始化粘?hello word : 85
系统初始化粘?hello word : 86
系统初始化粘?hello word : 87
系统初始化粘?hello word : 88
系统初始化粘?hello word : 89
系统初始化粘?hello word : 90
系统初始化粘?hello word : 91
系统初始化粘?hello word : 92
系统初始化粘?hello word : 93
系统初始化粘?hello word : 94
系统初始化粘?hello word : 95
系统初始化粘?hello word : 96
系统初始化粘?hello word : 97
系统初始化粘?hello word : 98
系统初始化粘?hello word : 99
系统初始化粘?hello word : 100
系统初始化粘?hello word : 101


作者: Hephaestus    时间: 2022-5-17 16:41
“正”汉字编码0xd5、0xfd,你这是遇到了C51的0xfd问题,网上找个补丁修一下就行了。
作者: yaoyao2022    时间: 2022-5-17 21:38
不建议printf 输出中文如果要建议UTF8 的格式输出。
作者: Homepea7    时间: 2022-5-17 22:04
建议输出英文或者换个串口助手,应该是解析中文最后一位的时候有问题
作者: jxcrgt35    时间: 2022-5-17 22:59
printf("系统初始化正\xfd常 hello word : %u\r\n",Cnt++);就可以了
作者: yzwzfyz    时间: 2022-5-19 11:12
函数会到汉字库中去取数据,取到什么就显示什么?
在取字模的过程序中,如果取字模的【索引】号错了,或索引到的地址中的点阵错了,就发生楼主遇到的问题了。
楼上的建议,或许有用。但希望你知道错在哪里。
作者: Hephaestus    时间: 2022-5-19 15:05
yzwzfyz 发表于 2022-5-19 11:12
函数会到汉字库中去取数据,取到什么就显示什么?
在取字模的过程序中,如果取字模的【索引】号错了,或索 ...

在网上搜“C51的0xfd问题”对你来说就那么困难吗?还要无理由的胡说八道。

就是C51的字符串中如果有汉字,而且汉字内码有0xfd,C51编译器会把0xfd吞掉,最后出来的代码里面没有0xfd自然就乱码了。
作者: LXJG    时间: 2022-5-19 23:14
格式弄一下就可以了

作者: yzwzfyz    时间: 2022-5-20 08:35
打开printf看看,是不是用先用汉字是索引字库,而后去取字模。
如果【C51编译器会把0xfd吞掉】是平台错了,还是楼主错了。
你如何证明,C51编译器会把0xfd吞掉了呢?亮出来给楼主看看,也算是帮楼主了。
作者: Hephaestus    时间: 2022-5-20 09:41
yzwzfyz 发表于 2022-5-20 08:35
打开printf看看,是不是用先用汉字是索引字库,而后去取字模。
如果【C51编译器会把0xfd吞掉】是平台错了 ...

在网上搜“C51的0xfd问题”对你来说就那么困难吗?还要无理由的胡说八道。
作者: 188610329    时间: 2022-5-20 14:03
如果:
        printf("系统初始化正常 hello word : %u\r\n",Cnt++);

换成:
       SendString("\r\n系统初始化正常 hello word : ");
        printf("%u",Cnt++);

是不会出现乱码的。
所以,问题不在字符串,不在汉字,也不在编译器,而是:
在明知KEIL C51 的 printf 不支持汉字,还非要用printf 发送汉字,就算没有现成的SendString,自己写一个
void SendString(char *p)
{
          while(*p)        SendUart(*p++);
}
就那么一句话的函数,很难么?

作者: Hephaestus    时间: 2022-5-20 15:01
188610329 发表于 2022-5-20 14:03
如果:
        printf("系统初始化正常 hello word : %u\r\n",Cnt++);

大错特错了你!

SendString("\r\n系统初始化正常 hello word : ");
这句话也是不正常的。

问题的根源不是printf不支持汉字,而是Keil C51编译器不支持字符串里面出现0xfd这个内码。你的字符串里面一样有这个内码,一样是不正常的,一样会出现乱码的。谦虚点儿吧你!
作者: spirit01    时间: 2023-10-15 15:09
printf支持汉字,Keil C51编译器不支持字符串里面出现0xFD,刚开始遇到的时候也想不通,监测后才发现是这个问题,这应该算是keil的bug吧
作者: Hephaestus    时间: 2023-10-15 16:09
spirit01 发表于 2023-10-15 15:09
printf支持汉字,Keil C51编译器不支持字符串里面出现0xFD,刚开始遇到的时候也想不通,监测后才发现是这个 ...

这个0xfd问题我在1999年就发表在当时最火的C51BBS网站上,过了24年还没有变,显然不是bug而是feature。




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1