gpt4 book ai didi

Contiki 时间戳值为 int

转载 作者:行者123 更新时间:2023-11-30 16:42:49 25 4
gpt4 key购买 nike

我想将模拟的时间戳值作为 int 发送。更清楚地说,如果节点输出是:

00:39.841   ID:6    unicast message ready to be sent

我希望能够在消息中输入值 00:39.841(以毫秒为单位)。我该怎么办?

谢谢。

最佳答案

我建议您使用函数 strtok()查找您需要的字符串,即 0039

strtol()从字符串到长整数的转换。不管怎样,请远离atoi()

Why shouldn't one use atoi() .

这里是示例,从分钟/秒到毫秒的转换由您决定。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char input[] = "00:39.841 ID:6 unicast message ready to be sent";
char * ptr;


long minutes = 0,
seconds = 0,
milisecs = 0;

ptr = strtok(input, ":");
if (ptr == NULL) { fprintf(stderr, "Wrong input minutes."); return 1; }
minutes = strtol(ptr, (char **)NULL, 10);

ptr = strtok(&input[3], ".");
if (ptr == NULL) { fprintf(stderr, "Wrong input seconds."); return 1; }
seconds = strtol(ptr, (char **)NULL, 10);

ptr = strtok(&input[6], " ");
if (ptr == NULL) { fprintf(stderr, "Wrong input milisecs."); return 1; }
milisecs = strtol(ptr, (char **)NULL, 10);

printf("%ld - %ld - %ld\n", minutes, seconds, milisecs);

return 0;
}

关于Contiki 时间戳值为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45714363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com