"Exits Funnel" <ex***************@yahoo.com> wrote in message
news:Y8*****************@fe12.lga...
Hello,
I've inherited a bunch of code which was written on windows and makes
frequent calls to _wtol( ) which converts a 2 byte char array to a long
integer.
My VC++ documentation says it converts a wide character
string to a long integer. Not the same thing.
I'm pretty sure it is a Microsoft extension.
Yes it is. None of the standard C++ library functions'
names begin with an underscore.
I'm porting the
code to Linux (g++) and I can't figure out how to replace it. It seems
I should be able to use the std::string class to transcode from wide
chars to chars and use atol( ) but I can't seem to make it work.
"Doesn't work" tells us nothing. You could show us your code if
you want us to help. But I can tell you why 'atol()' isn't the
right tool: it takes an argument of a 'regular' character string.
I recommend against it anyway, since there's no way to prevent
overflow causing UB. And since it indicates an 'error' by
returning zero, you can't distinguish between an error an
a valid zero-value conversion.
If
anyone has any thoughts I'd really appreciate it.
My first instinct would be to use a wstringstream:
#include <iostream>
#include <sstream>
int main()
{
wchar_t s[] = L"123";
std::wistringstream iss(s);
long value(0);
if(iss >> value)
std::cout << "value is " << value << '\n';
else
std::cerr << "Cannot convert\n";
return 0;
}
There's also 'wcstol()' (declared by <cstdlib>),
which will give a bit more control over error checking.
I'll let you look that one up yourself.
-Mike