将接收到的、格式化的 UDP 字符数组分配给 C 中的无符号整数
Assing received,formatted UDP char Array to unsigned integer in C
我通过 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?
像这样:
pwmValue0 = strtol(d, NULL, 10);
strtok
returns 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;
}
我通过 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?
像这样:
pwmValue0 = strtol(d, NULL, 10);
strtok
returns 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;
}