gpt4 book ai didi

c - 将接收到的、格式化的 UDP 字符数组分配给 C 中的无符号整数

转载 作者:太空宇宙 更新时间:2023-11-04 08:11:46 24 4
gpt4 key购买 nike

我通过 UDP 接收到一个字符数组并对其运行一些格式操作。工作正常。(它的格式为 dd:123)

我在 if 情况下使用“dd”。现在我需要将 123(保存在 d 中)保存在一个无符号整数 pwmValue0 中。

如果有人知道如何做到这一点,如果你能帮我一点我会很高兴

祝你有美好的一天!

recv(serverSocket, msg, sizeof(msg), 0);

printf("Here is the message: %s\n", msg);

char *c;
char *d;
c = strtok(msg, ":");
printf("token %s \n", c); //correct
d = strtok(NULL,".");
printf("token1 %s \n",d); //correct

if (strcmp(v0, msg) == 0) {
printf("Motortest\n");
printf("token2 %s \n",d); //correct
pwmValue0 = d; // How can I make this assignment?

最佳答案

strtok 返回 char* 并且您说您的 pwmValue0 是一个 unsigned int,因此您可以使用 atoi()

示例:

#include <stdio.h>
#include <stdlib.h>
int main(void) {
// your code goes here
char* p = "123";
int d;
d = atoi(p);
printf("%d",d);
return 0;
}

关于c - 将接收到的、格式化的 UDP 字符数组分配给 C 中的无符号整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38893221/

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