2

As the title indicates I need to know the best way to convert wchar_t* to long in visual c++. Is it possible to do that? If possible how to do that?

  • 3
    What do you mean by "covert"? Can you please exit your question to include example input and output? And what have you tried yourself? – Some programmer dude Aug 14 '15 at 09:34
  • 1
    Your question is unclear. At first I thought that you want to convert the `wchar_t` pointer to `long`, which is not "possible" on 64-bit Windows. Only after reading the answer I know that you want to convert the character string stored in `wchar_t*` to `long` – phuclv Aug 14 '15 at 10:48

2 Answers2

6

Use _wtol() to Convert a wide-character string to a long.

wchar_t *str = L"123";
long lng = _wtol(str);
Deadlock
  • 4,211
  • 1
  • 20
  • 25
  • 1
    `_wtol`? Really? Is that really the best way in C++? Is that even a C++ function or is it an MS specific function? Why do you link to CE docs. How are errors handled? Is it the case that `_wtol(L"foo") == _wtol(L"0")`? – David Heffernan Aug 14 '15 at 09:35
  • if you have a better way to do that you can mention here. :) – Deadlock Aug 14 '15 at 09:55
  • 1
    That's a rather weak response – David Heffernan Aug 14 '15 at 10:05
  • The lack of error reporting is a restriction that needs to be spelled out. If an application doesn't need error reporting here, then this may be a viable solution. For all other instances, [the other answer](http://stackoverflow.com/a/32007302/1889329) is superior, and should really be the accepted answer. – IInspectable Aug 14 '15 at 11:16
  • @IInspectable I wouldn't say my answer is necessarily superior as `_wtol` seems to run much faster. A quick profile on my machine showed that it runs in ~40ns versus ~100ns for the other methods. – sjdowling Aug 14 '15 at 15:18
  • @sjdowling: It all depends on the specific needs, of course. Inability to identify failures, however, is generally undesirable. I did point out, that your answer is superior, **if** error reporting is required. I suppose, we can agree on that part. – IInspectable Aug 14 '15 at 15:36
4

Use boost::lexical_cast.

#include <iostream>
#include <boost/lexical_cast.hpp>

int main()
{
    const wchar_t* s1 = L"124";
    long num = boost::lexical_cast<long>(s1);
    std::cout << num;
    try
    {
        const wchar_t* s2 = L"not a number";
        long num2 = boost::lexical_cast<long>(s2);
        std::cout << num2 << "\n";
    }
    catch (const boost::bad_lexical_cast& e)
    {
        std::cout << e.what();
    }
}

Live demo

Use std::stol.

#include <iostream>
#include <string>

int main()
{
    const wchar_t* s1 = L"45";
    const wchar_t* s2 = L"not a long";

    long long1 = std::stol(s1);
    std::cout << long1 << "\n";
    try
    {
        long long2 = std::stol(s2);
        std::cout << long2;
    }
    catch(const std::invalid_argument& e)
    {
        std::cout << e.what();
    }    
}

Live Demo.

Use std::wcstol

#include <iostream>
#include <cwchar>

int main()
{
    const wchar_t* s1  = L"123";
    wchar_t *end;
    long long1 = std::wcstol(s1, &end, 10);
    if (s1 != end && errno != ERANGE)
    {
        std::cout << long1;
    }
    else
    {
        std::cout << "Error";
    }
    const wchar_t* s2  = L"not a number";
    long long2 = std::wcstol(s2, &end, 10);
    if (s2 != end && errno != ERANGE)
    {
        std::cout << long2;
    }
    else
    {
        std::cout << "Error";
    }
}

Live Demo

I ran some benchmarks with 100 samples of each of these methods as well as _wtol converting the string L"123".

benchmark

sjdowling
  • 2,994
  • 2
  • 21
  • 31
  • [std::stol](http://en.cppreference.com/w/cpp/string/basic_string/stol) requires C++11. A solution based on [stringstream](http://en.cppreference.com/w/cpp/io/basic_stringstream)s can be used with pre-C++11 as well. This, too, can be implemented to report errors. – IInspectable Aug 14 '15 at 10:32
  • @IInspectable You can achieve this with stringstreams but I would consider them to be a last resort given how [horrifically slow](http://tinodidriksen.com/2010/02/07/cpp-convert-int-to-string-speed/) they [can be](http://www.boost.org/doc/libs/1_59_0/doc/html/boost_lexical_cast/performance.html). – sjdowling Aug 14 '15 at 10:47
  • Performance may not be everyone's primary goal, and *"horrifically slow"* may still be fast enough. Taking a dependency on boost may not be feasible in every scenario, and C++11 may not be available. Since SO is about general applicability, offering options alongside a rationale is usually a good idea. – IInspectable Aug 14 '15 at 10:52