Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino:itoa打印201,sprintf打印99_Arduino_Byte_Percentage_Uint8t_Itoa - Fatal编程技术网

Arduino:itoa打印201,sprintf打印99

Arduino:itoa打印201,sprintf打印99,arduino,byte,percentage,uint8t,itoa,Arduino,Byte,Percentage,Uint8t,Itoa,我很难用itoa()打印字节值(uint8_t),需要打印一定百分比的卷。我想使用这个函数,因为它减少了二进制大小 updateStats函数的两个版本(使用oled_I2C库在oled显示器上打印统计数据:oled显示器(SDA,SCL,8);): ITOA(不工作,打印V:201%) void updateStats() { char buff[10]; //the ASCII of the integer will be stored in this char array mems

我很难用itoa()打印字节值(uint8_t),需要打印一定百分比的卷。我想使用这个函数,因为它减少了二进制大小

updateStats函数的两个版本(使用oled_I2C库在oled显示器上打印统计数据:oled显示器(SDA,SCL,8);):

ITOA(不工作,打印V:201%)

void updateStats()
{
  char buff[10]; //the ASCII of the integer will be stored in this char array
  memset(buff, 0, sizeof(buff));

  buff[0] = 'V';
  buff[1] = ':';

  itoa( (uint8_t)getVolume() ,&buff[2],7 ); // get percent
  strcat( buff,"%" ); 

  display.print( getInputModeStr(), LEFT  , LINE3 );  
  display.print( buff, RIGHT , LINE3 );  
}
SPRINTF(按预期工作,打印V:99%)

void updateStats()
{
  char buff[10]; //the ASCII of the integer will be stored in this char array
  memset(buff, 0, sizeof(buff));

  buff[0] = 'V';
  buff[1] = ':';

  itoa( (uint8_t)getVolume() ,&buff[2],7 ); // get percent
  strcat( buff,"%" ); 

  display.print( getInputModeStr(), LEFT  , LINE3 );  
  display.print( buff, RIGHT , LINE3 );  
}
问题

void updateStats()
{
  char buff[10]; //the ASCII of the integer will be stored in this char array
  memset(buff, 0, sizeof(buff));

  buff[0] = 'V';
  buff[1] = ':';

  itoa( (uint8_t)getVolume() ,&buff[2],7 ); // get percent
  strcat( buff,"%" ); 

  display.print( getInputModeStr(), LEFT  , LINE3 );  
  display.print( buff, RIGHT , LINE3 );  
}
知道itoa()函数打印错误数字的原因吗?任何解决方案如何解决这个问题?

这一行
itoa((uint8_t)getVolume(),&buff[2],7);//获取百分比
错误

你要的是以7为基数的数字,而你要的是以10为基数的数字

下面是一个快速计算:

99÷7=14 r 1
14÷7=2 r 0
∴ 9910=2017

完整代码 更正的示例如下所示:

void updateStats()
{
  char buff[10]; //the ASCII of the integer will be stored in this char array
  memset(buff, 0, sizeof(buff));

  buff[0] = 'V';
  buff[1] = ':';

  itoa( (uint8_t)getVolume() ,&buff[2], 10 ); // get percent
  strcat( buff,"%" ); 

  display.print( getInputModeStr(), LEFT  , LINE3 );  
  display.print( buff, RIGHT , LINE3 );  
}

谢谢我认为第三个参数是缓冲区大小,这就是为什么我使用7(因为buff[10])而不是10。感谢allot,轻松修复并为我节省了4%的可执行空间(与使用sprintf而不是itoa相比)。你也可以使用utoa。