我有以下程序。奇怪的是,我期望为零,但程序认为它是 48。如果我调试这个程序,我可以看到分配为 48 的值,但是如果我打印“someVar”的值,它会正确显示为 0。但是如果我转换“someVar”到 uint16_t,然后一切正常。你能解释一下这个问题吗
#include "boost/cstdint.hpp"
#include <iostream>
#include "boost/lexical_cast.hpp"
using namespace boost;
int main(int argc, char ** args)
{
uint8_t someVar = lexical_cast<uint8_t> (args[1]);
if (someVar > 0)
cout << "greater than zero" << endl;
return 0;
}
最佳答案
字符零('0'
确实是 48,其中数字零作为整数,显然确实是零)。如果你制作 lexical_cast<int>
,它将执行您想要的操作,并将 args[1] 转换为字符串中的整数。自 uint8_t
是一个字符,我怀疑词法转换会按照您的要求进行操作,并为您提供字符串的第一个字符。
关于c++ - 如何在 boost::uint8_t 中存储零值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14595614/