C 或 C 中 64 位整数(uint64_t)的 atoi 等价于什么?可以在 Unix 和 Windows 上运行吗?
我正在尝试将 64 位整数字符串转换为整数,但我不知道该使用哪一个。
I'm trying to convert 64bit integer string to integer, but I don't know which one to use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
strtoulll
如果你有的话或者< a href="http://msdn.microsoft.com/en-us/library/85zk715d.aspx" rel="noreferrer">_strtoui64()
与 Visual Studio。Use
strtoull
if you have it or_strtoui64()
with visual studio.您已标记此问题c++ ,所以我假设您可能也对 C++ 解决方案感兴趣。如果您无法使用 boost,您可以使用
boost::lexical_cast
或std::istringstream
来执行此操作:这两种样式都适用于 Windows 和 Linux(以及其他)。
在 C++11 中还有 对
std::string
进行操作的函数,包括std::stoull
您可以使用:You've tagged this question c++, so I'm assuming you might be interested in C++ solutions too. You can do this using
boost::lexical_cast
orstd::istringstream
if boost isn't available to you:Both styles work on Windows and Linux (and others).
In C++11 there's also functions that operate on
std::string
, includingstd::stoull
which you can use:类似.....
然后只需使用
atoll()
。您可能想将#ifdef WINDOWS
更改为其他内容,只需使用您可以依赖的内容来指示atoll()
缺失,但atoi64()
就在那里(至少对于您关心的场景而言)。Something like...
..then just use
atoll()
. You may want to change the#ifdef WINDOWS
to something else, just use something that you can rely on to indicate thatatoll()
is missing butatoi64()
is there (at least for the scenarios you're concerned about).尝试
strtoul()
或strtoul()
。前者仅在 C99 和 C++11 中存在,但通常广泛可用。Try
strtoull()
, orstrtoul()
. The former is only in C99 and C++11, but it's usually widely available.在现代 C++ 中,我会使用 std::stoll。
http://en.cppreference.com/w/cpp/string/basic_string/stol
In modern c++ I would use std::stoll.
http://en.cppreference.com/w/cpp/string/basic_string/stol
在 strtoll(当然也很容易与 std::string 一起使用)和 std::stoll(乍一看似乎更适合 std::string)或 boost::lexical_cast 等 C 风格函数之间进行选择时:请注意,如果后两者无法解析输入字符串或范围溢出,则会抛出异常。
有时这很有用,有时没有,这取决于您想要实现的目标。
如果您无法控制要解析的字符串(因为它是外部数据),但您想编写健壮的代码(这始终是您的愿望),那么您始终需要预料到某些恶意攻击者注入的损坏数据或损坏的外部组件。对于损坏的数据,strtoll 不会抛出异常,但需要更明确的代码来检测非法输入数据。 std::stoll 和 boost::lexical_cast 会自动检测并发出蹩脚输入信号,但您必须确保在某处捕获异常以避免被终止(TM)。
因此,根据周围代码的结构、解析结果的需要(有时将非法数据“解析”为 0 是绝对可以的)、要解析的数据源以及最后但并非最不重要的您个人的需要来选择其中之一偏好。通常,这两种可用功能都不优于其他功能。
When choosing between C-style functions like strtoll (which are of course easy to use with std::string as well) and std::stoll (which at first glance appears better suited for std::string) or boost::lexical_cast: Be aware that the two latter will throw exceptions in case they cannot parse the input string or the range overflows.
Sometimes this is useful, sometimes not, depends what you're trying to achive.
If you are not in control of the string to parse (as it's external data) but you want to write robust code (which always should be your desire) you always need to expect corrupted data injected by some malicious attacker or broken outside components. For corrupted data strtoll will not throw but needs more explicit code to detect illegal input data. std::stoll and boost::lexical_cast do auto detect and signal crappy input but you must make sure to catch exceptions somewhere to avoid being terminated(TM).
So choose one or the other depending on the structure of the surrounding code, the needs of the parsed results (sometimes illegal data being "parsed" into a 0 is absolutely OK) the source of the data to parse and last but not least your personal preferences. Neither of the functions available is generally superiour to the others.
这里我们将由十六进制字符组成的字符串转换为 uint64_t 十六进制值。字符串的所有单个字符仅按一位转换为十六进制整数。例如以 10 为基数 -> String = "123":
所以像这样的逻辑用于将十六进制字符的字符串转换为 uint_64hex价值。
Here we convert String consisting of HEX character to uint64_t hex value. All individual characters of string is converted to hex integer ony by one. For example in base 10 -> String = "123":
So like this logic is used to convert String of HEX character to uint_64hex value.