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.
Int To String (char*)
C++: std::stringstream. Type-safe, flexible, generic, transparent, easy to use...
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
??=include<stdio.h>??=define m mainint 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
But seriously, folks, you probably want something like a conversion_cast function:
So you would say, for instance:
If you *really* want a char*, you use std::string::c_str().
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().
Exactly what lexical_cast does over on boost.
- Houdini
Edited by - Houdini on January 29, 2002 3:35:54 PM
- Houdini
Edited by - Houdini on January 29, 2002 3:35:54 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement