I have following program. Oddly, i expect zero, but the program thinks it is 48. If i debug this program, i can see the value assigned as 48, but if i print the value of “someVar”, it displays correctly as 0. But if i convert “someVar” to uint16_t, then everything works fine. Can you please explain the issue
#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;
}
The character zero (
'0'is indeed 48, where the number zero as an integer, obviously indeed IS zero). If you make alexical_cast<int>, it will do what you want, and convert the args[1] into an integer number from the string. Sinceuint8_tis a character, I suspect lexical cast does what you ask for, and give you the first character of the string.