Solved

int to String C Question

Posted on 2004-09-17
5
49,068 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
  • 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now