IntToString(char *s, int i) ???

What function for putting a number into a string?
victorlongAsked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
Thanks.
0
 
lucidityCommented:
itoa(int,char*,int); //last arg is 10 for base 10

example...
itoa(X,STR,10);
0
 
nietodCommented:
Itoa() is not a standard C++ function.  sprintf() is.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
nietodCommented:
You would do something like.

char Str[80];
int i = 45;

sprintf(Str,"%i",i);

of course ther are millions of possibilities for controling the base, the leading sign, the pading characters etc with sprintf().
0
 
victorlongAuthor Commented:
I want a C++ answer since a am in C++ space.

I remember function printf() is a C style function which needs a C style head file (forgot the name). Are you sure sprintf() is a C++ function? What head file it needs?
0
 
nietodCommented:
All the c functions are available in C++.  It is found in stdio.h.

I guess if you want a more C++ (not C) approach, you could use the << operator to a strstream, like

#include <strstream>
strstream S;
int i = 123;
char Str[50];

S << i;
strcpy(Str,S.str());

But I don't think I would go to that trouble.
0
 
nietodCommented:
Actually that's not 100% correct.  the string returned by the strstream's str() function is not NUL terminated.  You have to get the stringbuffer and get the length from there.  I can elaborate if you really want to go that route.
0
 
victorlongAuthor Commented:
Nietod,

1. So you agree that C approach is better than C++ in my case.
2. Why you said "Itoa() is not a standard C++ function.  sprintf() is."? It seems to me that Both of them are C functions and can be used in C++.

0
 
nietodCommented:
1.  the sprintf() (C) approach will probably slightly faster and more direct than the stringstream (C++) approach.  Realistically, you aren't likely to notice the difference.

2.  sprintf() is part of the standard C library.  All C and C++ compilers that are shipped with a copy of the standard library (wich is probably all compilers) will support sprintf().  Thus code that uses sprintf() is portable.  itoa() is not part of the standard C or C++ libraries.  Thus code that uses it may not be portable to other compilers.  If you don't plan on porting and if your compiler supports it, then it doesn't matter and you can use it.  (Or you can write your own itoa() for compilers that don't support it and use the existing itoa() on compilers that do.).
0
 
victorlongAuthor Commented:
Experts, Thank you.

Lase quesion:

lucidity's answer isn't a wrong answer but nietod's comments let me know much more....How to do with the points?
0
 
nietodCommented:
That's up to you.  If it were up to me, I think I would base it on the solution I chose to use.  That is, if I choose to use itoa(), I would give lucidity the points, if not, I would give them to the other expert.    But the choice is yours.  I offered the advice freely knowing well that most likely I would not get the points.
0
 
victorlongAuthor Commented:
Lucidity,

Would you say something?
0
 
paynCommented:
nietod:

>All the c functions are available in C++.  It is found in stdio.h.

If you want to pick nits, you're supposed to #include <cstdio>, not <stdio.h>, if you want the standard C stdio library in a C++ project. The only time you're supposed to use the old-style headers is in a mixed-language project. Of course nearly every compiler comes with a cstdio that just #includes stdio.h anyway, so it doesn't really make any difference...

And just to clarify one more tiny irrelevancy, the fact that sprintf is in <cstdio> means it's part of the standard C++ library as well as the standard C library...

And why 80 characters to represent an integer? In case someone's using a 256-bit platform?

0
 
nietodCommented:
I believe that according to the standard (now that there is one).  stdio.h is the typical C standard stdio.h include file.  The cstdio include file is a short include file that includes the stdio.h include file, but includes it inside the "std" namespace.  Thus you can include either, depending on how you feel about namespaces.  And I believe this is part of the standard.

Why 80 characters?  I ment 800 to be safe.  : - ) No fixed length string is every long enough.  
0
 
victorlongAuthor Commented:
To nietod:

I think you should get the points and I assume lucidity agree also....
Can you tell me how to pass the points to you?
0
 
nietodCommented:
Well, I'm not sure lucidity would agree.  But the choice is yours (and the client is always right).  

You can reject his answer and I'll submit a "dummy" answer.  I appreciate the fact that you are taking the time seen to it that the correct (in your opinion) expert gets the points.  Too many times clients just grade the proposed answer regardless of who gave the answer they prefer.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.