找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2641|回复: 13
打印 上一主题 下一主题
收起左侧

单片机C语言printf中文字符串乱码 求助

[复制链接]
跳转到指定楼层
楼主
ID:1025655 发表于 2022-5-17 16:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
代码部分
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

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:883242 发表于 2022-5-17 16:41 | 只看该作者
“正”汉字编码0xd5、0xfd,你这是遇到了C51的0xfd问题,网上找个补丁修一下就行了。
回复

使用道具 举报

板凳
ID:1026496 发表于 2022-5-17 21:38 | 只看该作者
不建议printf 输出中文如果要建议UTF8 的格式输出。
回复

使用道具 举报

地板
ID:1027424 发表于 2022-5-17 22:04 | 只看该作者
建议输出英文或者换个串口助手,应该是解析中文最后一位的时候有问题
回复

使用道具 举报

5#
ID:939553 发表于 2022-5-17 22:59 | 只看该作者
printf("系统初始化正\xfd常 hello word : %u\r\n",Cnt++);就可以了
回复

使用道具 举报

6#
ID:123289 发表于 2022-5-19 11:12 | 只看该作者
函数会到汉字库中去取数据,取到什么就显示什么?
在取字模的过程序中,如果取字模的【索引】号错了,或索引到的地址中的点阵错了,就发生楼主遇到的问题了。
楼上的建议,或许有用。但希望你知道错在哪里。
回复

使用道具 举报

7#
ID:883242 发表于 2022-5-19 15:05 | 只看该作者
yzwzfyz 发表于 2022-5-19 11:12
函数会到汉字库中去取数据,取到什么就显示什么?
在取字模的过程序中,如果取字模的【索引】号错了,或索 ...

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

就是C51的字符串中如果有汉字,而且汉字内码有0xfd,C51编译器会把0xfd吞掉,最后出来的代码里面没有0xfd自然就乱码了。
回复

使用道具 举报

8#
ID:1004455 发表于 2022-5-19 23:14 | 只看该作者
格式弄一下就可以了
回复

使用道具 举报

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

使用道具 举报

10#
ID:883242 发表于 2022-5-20 09:41 | 只看该作者
yzwzfyz 发表于 2022-5-20 08:35
打开printf看看,是不是用先用汉字是索引字库,而后去取字模。
如果【C51编译器会把0xfd吞掉】是平台错了 ...

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

使用道具 举报

11#
ID:624769 发表于 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++);
}
就那么一句话的函数,很难么?
回复

使用道具 举报

12#
ID:883242 发表于 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这个内码。你的字符串里面一样有这个内码,一样是不正常的,一样会出现乱码的。谦虚点儿吧你!
回复

使用道具 举报

13#
ID:1052053 发表于 2023-10-15 15:09 | 只看该作者
printf支持汉字,Keil C51编译器不支持字符串里面出现0xFD,刚开始遇到的时候也想不通,监测后才发现是这个问题,这应该算是keil的bug吧
回复

使用道具 举报

14#
ID:883242 发表于 2023-10-15 16:09 | 只看该作者
spirit01 发表于 2023-10-15 15:09
printf支持汉字,Keil C51编译器不支持字符串里面出现0xFD,刚开始遇到的时候也想不通,监测后才发现是这个 ...

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

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表