在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
Using sscanf on an Arduino gives a mismatch with const char * and return value always the same despite different values for the input
arduino IDE编译器非常宽容,并且允许将变量定义为"字节",这是一个uint8_t或无符号字符,它似乎正在运行。但它不会直接插入此测试代码。我正在使用Arduino IDE和PlatformIO来比较结果。为了调试,我将此代码插入 OnlineGBD C++模拟器。我需要提供代表二进制值的十六进制字节,就好像我像这样插入它一样:
const char s[] = { 0x37, 0x74, 0x37, 0 } ; 00110111,01110100,00110111,0 写文本包
我必须将模拟器中的"byte b[6]"更改为"char b[6]"才能使其运行,但 printf 看起来它给了我指针,而不是内存位置中的内容。我的第二个 Printf 尝试将"b"变量格式化为 %p 和 %x 进行测试,并在值前面加上 0x7ffe,因此 (void**(b 是 0x7ffexxxx,b 的值是 xxxxxxxxxx。
最后,nBytes 的返回值始终为"7"。它显然应该给我它找到的字节数。我想我在指针和字符之间感到困惑。谢谢。
void RegisterList::writeTextPacket(const char *s) volatile{
int nReg;
byte b[6];
int nBytes;
nBytes=sscanf(s,"%x %x %x %x %x %x",&nReg, b, b+1, b+2, b+3, b+4)-1; // return number of bytes
printf("%dn",nBytes);
printf("%d, %p, %x, %x, %x, %x, %x", nReg, (void**) b, b, b+1, b+2, b+3, b+4);
loadPacket(nReg,b,nBytes,0,1); // loadPacket(int nReg, byte *b, int nBytes, int rep, int flag)
}
成功!谢谢你的大力帮助。将"byte"更改为"uint8_t"(Arduino有一个"typdef uint8_t byte"来处理这个问题(并将%hhx替换为%x摆脱了警告。而这个测试代码:
const char *s="16 A9 69 55 C6 A8";
int nReg;
uint8_t b[6];
int nBytes;
nBytes=sscanf(s,"%d %hhx %hhx %hhx %hhx %hhx",&nReg, b, b+1, b+2, b+3, b+4)-1; // return number of bytes
printf("%dn",nBytes);
printf("%d, %X, %X, %X, %X, %X", nReg, *b, *(b+1), *(b+2), *(b+3), *(b+4));
给出这个正确的结果,并只计算整数 nReg 数后面的十六进制数:
5
16, A9,69, 55, C6, A8
相关文章:
- 从python中调用C++函数并获取返回值
- 为什么模板类中的对象不能返回值
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 返回值优化:显式移动还是隐式
- lock_guard是否保护返回值
- 调用CreateProcess()并获取字符串的返回值
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 查找 GCD:并非所有控制路径都返回值
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 如何从 std::thread 返回值
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 方法错误"not all control paths return a value"和方法不返回值
- 从 cin 读取不会返回整个输入字符串
- 如何读取 C++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- 对fread的返回值感到困惑
- 将空数组传递给函数,在该函数中输入值并返回值的示例
- 不返回用户输入的值的函数
- 为什么 fstream::tellg() 返回值会因输入文本文件中的换行符数而增大,当文件针对 Windows (r