Solved

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

Posted on 1998-08-17
16
610 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
Comment Utility
itoa(int,char*,int); //last arg is 10 for base 10

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

Expert Comment

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

Expert Comment

by:nietod
Comment Utility
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
 

Author Comment

by:victorlong
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:victorlong
Comment Utility
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
Comment Utility
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
Comment Utility
Lucidity,

Would you say something?
0
 
LVL 1

Expert Comment

by:payn
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 40 total points
Comment Utility
Thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 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

11 Experts available now in Live!

Get 1:1 Help Now