Re: [ROOT] Converting Int to String

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Fri Jul 04 2003 - 17:33:05 MEST


Hi Chris,

Chris Hayward <cchaywar@umich.edu> wrote concerning
  [ROOT] Converting Int to String [Fri, 4 Jul 2003 10:04:49 -0400 (EDT)] 
----------------------------------------------------------------------
> Hello,
> I would like to convert an integer value to string, i.e. 120 to "120", but
> it seems that the stdlib.h function itoa is not recognized. I am using
> ROOT 3.05/05 with gcc 2.96 on lxplus. Is there a ROOT function that will
> do the same thing as itoa?

In general, when converting from strings to numbers and back again.,
one can use the very cool class `std::stringstream', for example: 

  int main(int argc, char** argv) { 
    int     my_integer = 0;
    float   my_float   = 0;
    for (int i = 1; i < argtc; ++i) {
      if (argv[i][0] != '-') continue; 
      std::string       opt(argv[i]); 
      std::stringstream arg(argv[i+1]);
      if (opt[1] == '-') { 
        if      (!opt.compare("--help"))  show_help();
        else if (!opt.compare("--int"))   { arg >> my_integer; i++; }
	else if (!opt.compare("--float")) { arg >> my_float;   i++; }
	else { 
	  std::stringstream msg;
	  msg << "bad command line option: " << opt;
	  throw std::runtime_error(msg.str().c_str());
	}
      } 
      else {
        for (std::string::iterator j = opt.begin(); j != opt.end(); ++j) {
          switch (*j) {
	  case 'h':   show_help();                             break;
          case 'i':   arg >> my_integer; j = opt.end()-1; i++; break;
	  case 'f':   arg >> my_float;   j = opt.end()-1; i++; break;
	  default: {
	      std::stringstream msg;
	      msg << "bad command line option: " << opt;
	      throw std::runtime_error(msg.str().c_str());
  	    }
         }
        }
      }
    } 

    std::stringstream out; 

    out << "My integer was: " << my_integer << "\n" 
        << "My float was:   " << my_float;

	
    std::cout << out.str() << std::endl;

    return 0;
  }

Notice, that the functions

  template <typename T> 
  std::ostream &operator<<(ostream& o, const T& t); 

  template <typename T> 
  std::istream &operator>>(istream& i, T& t); 

are the ones that does the translations, which is nifty as it means
you'll reuse your code for that to do object<->string conversions.  

The I/O and part string part of the standard C++ library is very
_very_ cool (but it does take a bit getting-used-to). 

Caveat: I don't know if CINT supports std::stringstream (declared in
<sstream>), but in compiled code you should be good.  

Yours, 

 ___  |  Christian Holm Christensen 
  |_| |	 -------------------------------------------------------------
    | |	 Address: Sankt Hansgade 23, 1. th.  Phone:  (+45) 35 35 96 91
     _|	          DK-2200 Copenhagen N       Cell:   (+45) 24 61 85 91
    _|	          Denmark                    Office: (+45) 353  25 305
 ____|	 Email:   cholm@nbi.dk               Web:    www.nbi.dk/~cholm
 | |



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:13 MET