0

I want to convert a wstring to unsigned long long.

I am trying it like this:

ret.push_back(_wtol(s1));

The compiler throws this error:

No conversion available for "std::wstring" to const "wchar_t *"

This is the entire code:

vector<unsigned long long> splitW2(const wstring& uMain, const wstring& uSplitBy)
{
    vector<unsigned long long>ret;

    int iStart = 0;

    for (;;)
    {
        int iPos = uMain.find(uSplitBy, iStart);
        if (iPos == -1)
        {
            wstring s1;
            s1 = uMain.substr(iStart, uMain.size() - iStart);
            if (s1.size() > 0)
            {
                ret.push_back(_wtol(s1));//the error here is: There is conversion available for "std::wstring" to const "wchar_t *"
            }
            break;
        }
        else
        {
            wstring s2;
            s2 = uMain.substr(iStart, iPos - iStart);
            ret.push_back(s2);//this of course is also not possible.
            iStart = iPos + 1;
        }
    }
    return ret;
}
prapin
  • 6,395
  • 5
  • 26
  • 44
tmighty
  • 10,734
  • 21
  • 104
  • 218
  • 2
    If you want to obtain a C-style string from a C++ `std::string` instance, use the `.c_str()` member function. But, in C++, strongly prefer using something other than `wtol` to convert a string into a number. `std::stol` would be a good start. – Cody Gray - on strike Dec 27 '22 at 09:16
  • 1
    According to Microsoft's [documentation](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/atol-atol-l-wtol-wtol-l), `_wtol()` expects a `const wchar_t*` argument, but you are passing an `std::wstring`. Use `c_str()` to obtain `const wchar_t*` from the string object. – heap underrun Dec 27 '22 at 09:18

0 Answers0