473,586 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2265

"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::wistringst ream 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::wistringst ream 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
16456
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 someone can help Thanks
22
5462
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 codecvt, in Josuttis' "The Standard C++ Library", did not help, and I'm still awaiting delivery of Langer's "Standard C++ IOStreams and Locales". ...
15
2643
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 isalpha,isdigit,etc. functions from the C library and C++ locale stuff. To fully embrace i18n I'm wondering if we have to fully make the switch to everything being...
2
2327
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 thaanku all in advance. God bless u.
1
4549
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 requires Multibyte strings. When I convert the Wide char to Multibyte, in the debugger they are
5
1719
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 bCharValue As Byte, iCounter As Integer, lIndex As Long Dim lAccValue As Long, lTableValue As Long
11
3354
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. Normally I would do this using sprintf as so: sprintf(&buffer, "%lu", integer); The problem is that I will not know if the integer values I am...
8
11862
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
3995
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 functions strtol, strtod, and strtoul, defined in <cstdlib>, convert a null- terminated character string to a long int, double, or unsigned long. You can...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.