Solved

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

Posted on 1998-08-17
16
617 Views
Last Modified: 2008-02-01
What function for putting a number into a string?
0
Comment
Question by:victorlong
[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
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
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!

 

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 40 total points
ID: 1170613
Thanks.
0

Featured Post

Technology Partners: 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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

623 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