小白一枚,有大佬能在keil4实现atoi函数吗??或者精通51下lcd1602使用的兄弟,球球了。

想用51在lcd1602显示激光测距传感器的值,但是传输的值是“150mm”这样的字符串。想转为int型数组或者拆成单个字符也可以。

自己写吧,不难就是繁琐点,没测试你要检查下有没有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;

}

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m