Jump to content
  • Advertisement
Sign in to follow this  
programering

integer to string

This topic is 5464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to convert a integer to string, but the function prototype for itoa() wasn't as expected. How do I use it, what shall I pass in the arguments? char *itoa(int , char *, int); I thought it was: char *itoa(int); I only need to pass a integer value to itoa(int) to get the string. Why is it a char* and another int parameter?

Share this post


Link to post
Share on other sites
Advertisement

char szBuff[33];
int nNum = 42;
itoa(nNum,szBuff,10);



Its because you can convert to different based numbers. The char* parameter is the buffer that you want to store the result in. You should really just use sprintf(szBuff,"%d",nNum);, since itoa() is Microsoft only.

Share this post


Link to post
Share on other sites
Which language?

itoa() is not a valid C or C++ function. At a guess the function prototype is


itoa(int number, //the number you want as a string
char* string, //the string you want the number to be put in
int radix) //the radix of the number in the string



But as i say, that is not a portable function in C or C++.

Stringstreams provide an alternative in C++.


#include<sstream>
std::stringstream ss;
ss << myint;
std::string mystringnumber = ss.str();


You could also use boost::lexical_cast in C++.

EDIT: Corrected 3rd param

Share this post


Link to post
Share on other sites
The third parameter of itoa() is actually the radix in which to represent the number in the final string.

So, itoa(123, string, 8) will put 123 in octal into the string.

-Auron

EDIT: Ugh, didn't quote so my statement was all out of context...

Share this post


Link to post
Share on other sites
If you're converting a number in base 10, the maximum value you could have is 4294967295, so if you allocate space for 11 characters you'll be fine. Theres no point in working out the exact size to allocate when you're dealing with sizes so small, unless you intend to make 100000+ allocations

Share this post


Link to post
Share on other sites
   If you don't like the sprintf or itoa functions because you have to pre-allocate your own string a head of time(that might be significantly bigger then the int you want to represent) then wright your own function along the lines of this(This may be a little incorrect as I am making this on the fly right now):
--------------------------------------------------------
// This function assumes base 10 output
char *IntToString(int Num)
{
 int i = 0;
 int TempNum = Num;
 char *OutputString = NULL;

 // Count how manyt numbers(Yes, this type of for loop works!)
 for(i=0; TempNum; i++)
   TempNum = TempNum/10;

 // NULL ending character
 ++i;

 // Allocate memory
 if ((OutputString = (char *)malloc(sizeof(char *)*i)) == NULL)
   return NULL;

 // Print the number
 sprintf(OutputString, "%d", Num);

 return OutputString;
}
-------------------------------------------------------------

   Just be sure to free the string when you're no longer using it... unless you want a memory leak; heck, this could maby even be part of a custom string class, then you wouldn't even need to worry about freeing it ;).

-Linolium

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
If you're converting a number in base 10, the maximum value you could have is 4294967295, so if you allocate space for 11 characters you'll be fine. Theres no point in working out the exact size to allocate when you're dealing with sizes so small, unless you intend to make 100000+ allocations


Mmmmm

What about "-1000000000\0" (12 char) ? and what about a future implementation of itoa() that may use thousands separators in base 10 ?

The only safe way I see is to go with the STL - as stated by elementary and petewood. Another safer alternative is to compute how much memory you'll need in order to store the integer value, or to use the maximum size that can be output by itoa() (ie when you want to create a base 2 number from a 32 bit integer: 32+1 char, as you stated in your first post). You must be aware that the latest is opened to buffer overrun exploits - but this is another story :)

Quote:
Original post by Linolium

// Count how many numbers (Yes, this type of for loop works!)
for(i=0; TempNum; i++)
TempNum = TempNum/10;

// NULL ending character
++i;



I'll add (at least):

// leading minus sign
if (Num < 0) {
++i;
}


Regards,

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
What about "-1000000000\0" (12 char) ? and what about a future implementation of itoa() that may use thousands separators in base 10 ?

Whoops, I forgot about the negative sign...

I agree that the best way to go is with the STL - if this is a C++ application, and not plain C.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!