Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > converting wide strings to long

Reply
Thread Tools

converting wide strings to long

 
 
Exits Funnel
Guest
Posts: n/a
 
      01-14-2005
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

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-15-2005

"Exits Funnel" <(E-Mail Removed)> wrote in message
news:Y8ZFd.6547$(E-Mail Removed)...
> 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




 
Reply With Quote
 
 
 
 
Exits Funnel
Guest
Posts: n/a
 
      01-16-2005
Mike Wahler wrote:
> "Exits Funnel" <(E-Mail Removed)> wrote in message
> news:Y8ZFd.6547$(E-Mail Removed)...
>
>>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
Converting long long to char silangdon C Programming 8 03-22-2005 03:57 PM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments