converting wide strings to long

Discussion in 'C++' started by Exits Funnel, Jan 14, 2005.

  1. Exits Funnel

    Exits Funnel Guest

    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
     
    Exits Funnel, Jan 14, 2005
    #1
    1. Advertising

  2. Exits Funnel

    Mike Wahler Guest

    "Exits Funnel" <> wrote in message
    news:Y8ZFd.6547$...
    > 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
     
    Mike Wahler, Jan 15, 2005
    #2
    1. Advertising

  3. Exits Funnel

    Exits Funnel Guest

    Mike Wahler wrote:
    > "Exits Funnel" <> wrote in message
    > news:Y8ZFd.6547$...
    >
    >>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
     
    Exits Funnel, Jan 16, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Web Developer

    char 8bit wide or 7bit wide in c++?

    Web Developer, Jul 31, 2003, in forum: C++
    Replies:
    2
    Views:
    589
    John Harrison
    Jul 31, 2003
  2. silangdon

    Converting long long to char

    silangdon, Mar 21, 2005, in forum: C Programming
    Replies:
    8
    Views:
    404
    Richard Bos
    Mar 22, 2005
  3. Disc Magnet
    Replies:
    2
    Views:
    719
    Jukka K. Korpela
    May 15, 2010
  4. Disc Magnet
    Replies:
    2
    Views:
    793
    Neredbojias
    May 14, 2010
  5. Martin Rinehart

    80 columns wide? 132 columns wide?

    Martin Rinehart, Oct 31, 2008, in forum: Javascript
    Replies:
    16
    Views:
    181
    John W Kennedy
    Nov 13, 2008
Loading...

Share This Page