Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

int to String C Question

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

Independent Software Vendors: 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

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…
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 how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

610 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