LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   int to string+sendto (https://www.linuxquestions.org/questions/programming-9/int-to-string-sendto-219493/)

pantera 08-18-2004 09:06 PM

int to string+sendto
 
i have a variable ts which is the string length of the messages that i have to send using sendto().but i cant directly send ts as it is an integer. i need to convert ts into a char or a string so that i can send it using the sendto() function shown below

int sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen);
how can i convert it??

itsme86 08-18-2004 09:35 PM

The message can be of any type...just pass &ts to sendto().

If you want to convert an int to a string then 'man sprintf'. That's one way anyway.

sibtay 08-19-2004 12:22 AM

sprintf help:

char *str=(char*)malloc(10);

char *mess="Hello";

int num=20;

sprintf(str,"Mess : %s Number: %d",mess,num)

The resuling string would be

str=Mess : Hello Number : 20

To concatenate a string you need to write %s

and to concatenate an integer you need to write %d

tonyfreeman 08-19-2004 07:19 PM

I had the same question yesterday under the title g_string_append problem. Although I didn't know that my problem was assigning a double to a string until a responder pointed it out. Here's the answer I finally found to work:

Code:

gchar *string_start;
gint cmin_start = 1440;

/* assign the integer to a string */
sprintf(string_start, "%d", cmin_start);

g_print("%s\n", string_start);

--Tony


All times are GMT -5. The time now is 08:51 PM.