Solved

int to String C Question

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best UNIX-compatible free C compiler for Windows or Mac 6 244
C hashtable library 3 90
why "." vs "->" 23 120
How to learn Linux? 10 60
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…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

785 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