Conversions in C++?

benjy355

New member
Blarg, because of the way I'm coding something, I need to know how I would convert a float, and integer into a string...
Not a char >_>, a string. I've learned that.
 
Here are two methods:

Example one. Use overloading method to convert
Code:
#include<iostream> 
#include<strstream> 
#include<string> 

using namespace std; 

template <class T> string convert2String (T value) { 
   ostrstream outs; 
   outs << value << ends; 
   return ( outs.str() ); 
} 
int main(int argc, char *argv[]) { 
   string x1 = convert2String ( (double) 3.414 ); 
   string x2 = convert2String ( (float) 1.23e33 ); 
   string x3 = convert2String ( (int)25 ); 
   cout << x1 << " " << x2 << " " << x3 << endl; 

   return 0; 
}

Example two -- Using sprintf

Code:
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#define NUM_OF_DIGITS 28
 
int main(int argc, char* argv[]) {
  double value=(sin(45));
  long long ival = 0x123456781234578LL;
  char buf[NUM_OF_DIGITS];
 
  sprintf(buf,"%.*g",NUM_OF_DIGITS,value);
  std::cout << buf << std::endl;

  // Same as above but let's only show 5 digits
 
  sprintf(buf,"%.*g",5,value);
  std::cout << buf << std::endl;
 
  // Long Long to string conversion
  sprintf(buf,"%llx", ival);
  std::cout << buf << std::endl;

  return 0;
}

I am sure somone will have a better take on the C++ version but it is late and I need to go to bed.. So a quick version is what I give you tonight!
 
sprintf FTW! ... if you're not working on an small embedded system then you shouldn't worry about cost etc...

That said - you should become familiar printf, sprintf and fprintf - all great functions if you ask me !

Good Luck!
 
ugh *printf *scanf :no:
use C++ stream classes for all input and output needs.

Here are two methods:

Example one. Use overloading method to convert
Code:
#include<iostream> 
#include<strstream> 
#include<string> 

using namespace std; 

template <class T> string convert2String (T value) { 
   ostrstream outs; 
   outs << value << ends; 
   return ( outs.str() ); 
} 
int main(int argc, char *argv[]) { 
   string x1 = convert2String ( (double) 3.414 ); 
   string x2 = convert2String ( (float) 1.23e33 ); 
   string x3 = convert2String ( (int)25 ); 
   cout << x1 << " " << x2 << " " << x3 << endl; 

   return 0; 
}
don't return strings. you might overload the stack! pass strings (and vectors and queues and other such encapsulated data structures) by reference.
 
Last edited:
It is really amazing how many C programmers there still are today. I've been using the itoa function since 1982. It's hard to believe that these things haven't been replaced yet.
 
It is really amazing how many C programmers there still are today. I've been using the itoa function since 1982. It's hard to believe that these things haven't been replaced yet.
I LOVE C !! (embedded world ... it's clean, fast, and if you're careful, it's perfectly safe)
 
And if you take a little care, it compiles and runs on a multitude of platforms
without having to adjust it
I LOVE THAT about POSIX ... I count 7 different platforms from full sized rigs to small hand held devices to small pocket sized devices ... all running the same code...

*hugs Linux* *hugs C*
 
The only bad thing about using C for cross-platform development is that you need to use specific ANSI C/C++ and do not use any specialized libraries. Because of this, Java is typically the better cross-platform development environment.
 
The only bad thing about using C for cross-platform development is that you need to use specific ANSI C/C++ and do not use any specialized libraries. Because of this, Java is typically the better cross-platform development environment.
hehe -- it depends on the platform ;)
 
Back
Top