想用51在lcd1602显示激光测距传感器的值,但是传输的值是“150mm”这样的字符串。想转为int型数组或者拆成单个字符也可以。
2条回答 默认 最新
- dceacho 2021-06-18 18:15关注
自己写吧,不难就是繁琐点,没测试你要检查下有没有BUG
//不考虑负数 long str2int(char* str) { char tmp8[12]={0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff} char startindex,lop; long ret=0; for(startindex=0;startindex<11;startindex++) { if(str[startindex]<='9' && str[startindex]>='0')break;//数字可能不在最前面 if(str[startindex]==0)return 0;//ascii 结束符'\0' } //最长限制11个数字 for(lop=0;lop<11;lop++) { if(str[startindex+lop]<='9' && str[startindex+lop]>='0')//主要就是这里ASCII码转成数字 { tmp8[lop]=str[startindex+lop]-'0'; } else break; } //数字是倒着存的 for(startindex=11;startindex>=0;startindex--) { if(tmp8[startindex]!=0xff)break; } for(lop=startindex;lop>=0;lop--) { ret*=10; ret+=tmp8[lop]; } return ret; }
解决 1无用
悬赏问题
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记
- ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
- ¥15 保护模式-系统加载-段寄存器
- ¥15 电脑桌面设定一个区域禁止鼠标操作
- ¥15 求NPF226060磁芯的详细资料