473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

converting wide strings to long

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

Jul 22 '05 #1
2 2255

"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


Jul 22 '05 #2
Mike Wahler wrote:
"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


Thanks Mike,

I think that wcstol was what I was looking for.

-exits

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
22
by: Keith MacDonald | last post by:
Hello, Is there a portable (at least for VC.Net and g++) method to convert text between wchar_t and char, using the standard library? I may have missed something obvious, but the section on...
15
by: Steve | last post by:
Hi, I've been charged with investigating the possibilities of internationalizing our C++ libraries. std::strings are used all over the place, and unfortunately a mixture of...
2
by: XML newbie: Urgent pls help! | last post by:
Does anyone have a snippet of code that will convert a string to a long array? I've nearly smashed my head against the wall trying to figure this out. I'm Using vb.net 2005 Pls reply asap. I...
1
by: miner49er | last post by:
Hi there, Here's my problem, please help - I think i'm going insane :-) I have written a DLL that returns Wide Char Unicode Chinese Strings. I have a 3rd party Graph control (OCX) that...
5
by: jamie | last post by:
I'm having a hell of a time figure out how to translate this piece of code. Public Function AdDDNc32(ByVal Item As String, ByVal Crc32 As Long) As Long 'Declare following variables Dim...
11
by: TomServo | last post by:
I am writing code that needs to run on a variety of Unix systems. I am calling the statvfs and statfs system calls and I need to to convert some of the integers returned to character strings....
8
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.