Topic: wchar_t version of atof() and atoi()?


Author: jamshid@io.com (Jamshid Afshar)
Date: 1996/02/29
Raw View
I'm new to internationalization issues but it's my understanding that
Standard C++ will have very good support for it by way of the
wide-character wstring and wifstream, etc. classes.  For example, a
C++ program written in an environment that supports Unicode would
strictly use wstring, wifstream, and wchar_t in place of string,
ifstream, and char [1].  But, I didn't see a wchar_t* overload of
functions like strtod() or atoi().  How are strings (eg, entered by a
user into an edit box) supposed to be converted to numeric values in
an internationalized C++ program?

I would do something like:

 int atoi( const wstring& s ) {
    wistringstream strm(s);
    int r;
    s >> r;
    if (s) return r;
    else throw some_exception();
 }

But stream i/o interprets "045" as an octal value instead of 45.

Also, it seems that if you want to write code that is portable to
either a Unicode compiler/environment or a regular single-byte string
environment you would have to introduce your own conditional typedefs
for the string and stream classes, and consistently use those typdefs
instead of directly using "string" or "wstring".  Does that sound
right, or is there a technique using namespaces to solve this problem?

[1] It seems there's even "w" versions of the cin/cout (win/wout) and
the argv char*'s are allowed to be multi-byte character strings which
can be converted to wchar_t strings.

Jamshid Afshar
jamshid@io.com
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]