Re: [gtkmm] Glib::ustring -> int



An easier way is to use the very flexible "lexical_cast" function from the
C++ Boost library.  See
  http://www.boost.org/libs/conversion/lexical_cast.htm
It uses a string stream and also handles errors (by throwing an exception).

The syntax would then be:

  Glib::ustring s("123456");
  long x = boost::lexical_cast<long>(s);

Dan


----- Original Message -----
From: "Robert J. Hansen" <rjhansen inav net>
To: "Benjamin A. Cooper" <bacooper bacwater net>
Cc: <gtkmm-list gnome org>
Sent: Wednesday, September 04, 2002 8:41 PM
Subject: Re: [gtkmm] Glib::ustring -> int


> > How do I get an int from the string (and a float as well)?
>
> First: you don't.  atoi() is part of C++'s C legacy; there are better
> ways to do the same task in C++.
>
> Second: use ustring::raw().  It will return a const std::string
> reference.  You can then throw it into a stringstream and get it back
> out again later as an int.  Code snippet follows.
>
> =====
>
> Glib::ustring myASCIIdata("123456");
> std::stringstream s;
> long result;
>
> s << myASCIIdata.raw();
> s >> result;
>
> =====
>
> Presto.  myASCIIdata has been fed into a stringstream and extracted
> again into result.  If you're comfortable with stringstreams, this is by
> far the preferred solution.
>
>
> --
> Geek Code: GAT d- s+:+ a27 C++(+++)$ ULB++>++++ P++ L+++>++++ E W+ N+ w
>            PS+ PE++ Y++ PGP++ t+ 5++ X-- R tv b+++ DI++ D--- G+ e++ h*
>            r* y+*
>
> _______________________________________________
> gtkmm-list mailing list
> gtkmm-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtkmm-list
>
>





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]