converting wide strings to long

E

Exits Funnel

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. I'm pretty sure it is a Microsoft extension. 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. If
anyone has any thoughts I'd really appreciate it. Thanks in advance.

-exits
 
M

Mike Wahler

Exits Funnel said:
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
 
E

Exits Funnel

Mike said:
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

Thanks Mike,

I think that wcstol was what I was looking for.

-exits
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top