b4shyou b4shyou - 1 year ago 110
C Question

Assing received,formatted UDP char Array to unsigned integer in C

I receive a char Array via UDP and run some format operations on it. Works fine.(it has the form dd:123)

I use the "dd" for my if cases. Now I need to save the 123 (saved in d)in an unsigned integer pwmValue0.

If anybody knows how to do this I would be very happy if you could help me out a little

Have a nice day!

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("token2 %s \n",d); //correct
pwmValue0 = d; // How can I make this assignment?

Answer Source

strtok returns char* and you said your pwmValue0 is an unsigned int, so you can use atoi()


#include <stdio.h>
#include <stdlib.h> 
int main(void) {
    // your code goes here
    char* p = "123";
    int d;
    d = atoi(p);
    return 0;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download