C++ 将字符串转换为 long long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/303759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:27:34  来源:igfitidea点击:

convert string to long long

c++stringvisual-studio-2008

提问by Alonso

I'm using VS 2008 to create a C++ DLL (not managed) project and I need convert a char* to a long long type. Is there an easy way to do it?

我正在使用 VS 2008 创建一个 C++ DLL(非托管)项目,我需要将 char* 转换为 long long 类型。有没有简单的方法来做到这一点?

Thanks in advance :)

提前致谢 :)

采纳答案by altruic

Try _atoi64.This takes char*and returns __int64.

Try_atoi64.这需要char*并返回__int64

回答by Alan

The easiest way is to use the std::stringstream (it's also the most typesafe...)

最简单的方法是使用 std::stringstream (它也是最类型安全的......)

std::stringstream sstr(mystr);
__int64 val;
sstr >> val;

You may need to target a 64-bit application for this to work.

您可能需要以 64 位应用程序为目标才能使其工作。

C++ FAQ

C++ 常见问题

回答by Ryan Ginstrom

If you're using boost, lexical_cast is the way to go, in my opinion.

如果您使用的是 boost,我认为 lexical_cast 是您要走的路。

long long ll = boost::lexical_cast<long long>(mystr)

回答by Keith Thomas

Another option is using stoll() found within the string library. Takes a C++ string.

另一种选择是使用字符串库中的 stoll() 。接受一个 C++ 字符串。

long long ll = std::stoll(mystr);

Docs

文档