Convert std :: string to MSVC specific __int64

Can I find out how I can convert std :: string to MSVC specific __int64?

+3
source share
3 answers

_ atoi64, _atoi64_l, _wtoi64, _wtoi64_l

std::string str = "1234";
__int64 v =_atoi64(str.c_str());

See also this link (although this is for linux / unix): Why does C ++ not override standard C functions with C ++ elements / styles?

+5
source

Here is one way:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {
    string s( "1234567890987654321");

    stringstream strm( s);

    __int64 x;

    strm >> x;

    cout << x;

}
+3
source

__int64, - . , .

Boost lexical cast - . :

__int64 x = boost::lexical_cast<__int64>("3473472936");

boost, . , :

template <typename R>
const R lexical_cast(const std::string& s)
{
    std::stringstream ss(s);

    R result;
    if ((ss >> result).fail() || !(ss >> std::ws).eof())
    {
        throw std::bad_cast();
    }

    return result;
}

It performs some additional functions, such as checking for trailing characters. ( "123125asd"will fail). If the throw cannot be made, it is thrown bad_cast. (Like boost.)

Also, if you have access to boost, you can avoid the need to use an __int64MSVC-enabled extension with

#include <boost/cstdint.hpp>
typedef boost::int64_t int64;

Get int64on any platform that provides it, without changing the code.

+1
source

Source: https://habr.com/ru/post/1729863/


All Articles