Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1998-08-17
16
Medium Priority
?
620 Views
Last Modified: 2008-02-01
What function for putting a number into a string?
0
Comment
Question by:victorlong
16 Comments
 
LVL 2

Expert Comment

by:lucidity
ID: 1170598
itoa(int,char*,int); //last arg is 10 for base 10

example...
itoa(X,STR,10);
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170599
Itoa() is not a standard C++ function.  sprintf() is.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170600
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:victorlong
ID: 1170601
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
 
LVL 22

Expert Comment

by:nietod
ID: 1170602
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
 
LVL 22

Expert Comment

by:nietod
ID: 1170603
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
 

Author Comment

by:victorlong
ID: 1170604
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
 
LVL 22

Expert Comment

by:nietod
ID: 1170605
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
 

Author Comment

by:victorlong
ID: 1170606
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
 
LVL 22

Expert Comment

by:nietod
ID: 1170607
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
 

Author Comment

by:victorlong
ID: 1170608
Lucidity,

Would you say something?
0
 
LVL 1

Expert Comment

by:payn
ID: 1170609
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
 
LVL 22

Expert Comment

by:nietod
ID: 1170610
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
 

Author Comment

by:victorlong
ID: 1170611
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
 
LVL 22

Expert Comment

by:nietod
ID: 1170612
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
 
LVL 22

Accepted Solution

by:
nietod earned 160 total points
ID: 1170613
Thanks.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

824 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