?
Solved

int to String C Question

Posted on 2004-09-17
5
Medium Priority
?
49,113 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 80 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 120 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

771 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