sscanf()的问题不会读取char数组中的每个0

问题描述 投票:1回答:1

我需要从我的char数组中提取数字,它以hh:mm的格式存储值(示例20:20)我尝试使用sscanf函数将hh提取为小时变量,将mm提取为分钟变量。直到时间类似于0number:0number或如果为00:00为止,它运行良好。它仅返回不带0或仅包含0的数字。是否有可能在读取第一个0时将其当作某物否则,不是数组值的一部分?谢谢您的回答。

char time[15]; ///where I store the time value 
Serial.println(time); //prints nicely something like 02:02
int hour;
int minute;

sscanf(incas,"%02d:%02d",&hour,&minute); 
Serial.println(hour);  ///prints 2
Serial.println(minute); ///prints 2

c++ scanf arduino-ide
1个回答
0
投票

int不知道前导零。 0000000000000123 == 123。如果要使用前导零,则必须自行设置其格式。

hourminute转换回char数组的示例:

char out[15];
sprintf(out, "%02d:%02d", hour, minute);
Serial.println(out);
© www.soinside.com 2019 - 2024. All rights reserved.