我正在尝试通过串行通信与 USB 加密狗进行通信。我的通信正常,但是我无法让设备正确解析通信。我的设备读取消息并将其与硬编码的 c 字符串进行比较。它解析并识别出它是正确的字符串,但是当我尝试解析 : 字符之后的值时,它返回 0x00000000 我不知道为什么。我尝试使用 char cast 并使用 atoi,我尝试使用简单的 ascii 翻译,甚至进行按位加法运算,如下所示:将 vector<uint8_t> 的子集转换为 int
例如:
我发送“心率:55”它解析并识别出“心率:”但是当我告诉它去寻找 55 并将它带回来做一些事情时,它给了我一个 0x00000000 这
是一个片段:
const uint8_t hrmSet[] = "Heart Rate:";
/** Find the : character in the string and break it apart to find if it matches,
and determine the value of the value of the desired heart rate. **/
int parse(uint8_t *input, uint8_t size)
{
for (uint8_t i = 0; i < size; i++)
{
if (input[i] == ':')
{
if (compare_string(input, hrmSet, i) == 0)
{
int val = 0;
for (int j = i+1; j < size; j++)
{
if (!isdigit(input[j]))
{
for (int k = i; k < j; k++)
{
val <<= 8;
val |= input[k];
}
}
}
return val;
}
return -1;
}
}
return -1;
}
比较字符串函数
/** Compare the input with the const values byte by byte to determine if they are equal.**/
int compare_string(uint8_t *first, const uint8_t *second, int total)
{
for (int i = 0; i < total; i++)
{
if (*first != *second)
{
break;
}
if (*first == '\0' || *second == '\0')
{
break;
}
first++;
second++;
}
if (*first == ':' && *second == ':')
{
return 0;
}
else
{
return -1;
}
}