Solved

int to String C Question

Posted on 2004-09-17
5
49,108 Views
Last Modified: 2012-06-22
Hi,

   How do I convert an integer to a string? Easiest coded example will be awarded.

I have an integer, p

want to convert p to string and send it using the following send command:

send(s, STRING, strlen(STRING), 0)

where s is a socket and STRING is int p converted to a string.
Please provide the correct code to send this int converted to string, as short and precise as possible.

0
Comment
Question by:jameswalt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 20 total points
ID: 12090533
Use the itoa() function
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12090535
Here's an example:
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
   int number = 12345;
   char string[25];

   itoa(number, string, 10);
   printf("integer = %d string = %s\n", number, string);
   return 0;
}
0
 
LVL 3

Accepted Solution

by:
CmdrRickHunter earned 30 total points
ID: 12090551
itoa is not portable.  If you have it, it is by far the most simple solution.

If you need more portable, use sprintf.

int n = 5;
char buffer[10]; /* long enough to hold your number + null */
sprintf(buffer, "%i", n);

which works should work on any system with stdio.

Also you can format it using the normal % encoding
sprintf(buffer, "%.2f", floatNumber);
for example




if you're looking to send it over a socket, you can always send it raw
send(socket, &theInt, sizeof(int));
just watch out for byte order if you're going across platforms.  hton will change the byte ordering to a "standard" networking order, which I believe is big endian
0
 

Author Comment

by:jameswalt
ID: 12090552
Getting undefined symbol. I have the files included.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12090588
Yeah,you can use sprintf() in case you dont have itoa().

sprintf() works like printf and writes to a string instead of standard output.

CmdrRickHunter has already provided an example.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question