Archived

This topic is now archived and is closed to further replies.

Anonymous0

Int To String (char*)

Recommended Posts

itoa(int, char*, int)

first int is the int to convert to char.
char* is the buffer to put the converted string into.
the second int is the number base to convert the int from. Base 10 for stanard numbers. Base 2 for binary, base 8 for oct, base 16 for hex.

Share this post


Link to post
Share on other sites
Radix is what base you want your number to be viewed as.

IE, base 10 is decimal, 16 is hexidecimal, 8 is octal.


- Houdini

Share this post


Link to post
Share on other sites
radix is the number base.

radix = 2; would for binary numbers. base 2.
radix = 10; would be for decimal numbers. base 10.
radix = 16; would be for hexadecimal numbers. base 16.

etc. etc.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Just for further info, there is a whole slew of conversion functions that follow a standard format.

atoi is basically a char to int function (a is for char)


There''s also an itoa function (int to char)
atof function (char to float)
ftoa function (float to char)
l for long
d for double
etc.

Share this post


Link to post
Share on other sites
using snprintf is safer than sprintf (which should be banned IMHO).

Watch those buffer overflows people, that''s how security holes are created ! Learn to do it right from the beginning.

Share this post


Link to post
Share on other sites
actually "atof" operates on "double". so you''ll have to explicitly type cast.

also there is "strtol", "strtoul", and "strtod".

and under MSVC++ there is "_i64toa" for 64-bit integers. tho, this is NOT ANSI.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
    
??=include<stdio.h>
??=define m main
int m(int c,char**g)??<static char n??(??)="0123456789abcdefghijklmnopqrstuvwxyz ";int o=sizeof(n)/sizeof(*n)-1;switch(c++)??<case 1:for(;(printf("Please enter a number (0 to exit): "),scanf("%d",(int*)g),*(int*)g);1??(g??)=(char*)16,printf("That number in hex: %s\n",(char*)m(c,g)),1??(g??)=(char*)2,printf("That number in binary: %s\n",(char*)m(c,g)));break;default:int v=(int)0??(g??),b=(int)1??(g??);if(!((0==b)??!??!(b>36)))do(--o)??(n??)=(v%b)??(n??);while((v/=b));??>return(int)(n+o);??>

Edit: Some additional functionality was added. The routine will convert to any base from 2 to 36, but doesn't handle negative numbers properly; that requires inelegant special-casing.

Edited by - DrPizza on January 29, 2002 3:37:58 PM

Share this post


Link to post
Share on other sites
But seriously, folks, you probably want something like a conversion_cast function:
  
template<typename O, typename I>
O conversion_cast(const I& rhs)
{
std::stringstream ss;
ss << rhs;
O output;
ss >> output;
return output;
}

So you would say, for instance:
  
std::string myString = conversion_cast<std::string>(myInt);

If you *really* want a char*, you use std::string::c_str().

Share this post


Link to post
Share on other sites