strtol

Convert a string to int (But only if really is an int)

In college I was asked if our program detects if the string enter from command line arguments is a integer which it did not(./Program 3.7). Now I am wondering how I can detect this. So input as for example a is invalid which atoi detects, but input like for example 3.6 should be invalid but atoi will convert this to an integer. #includ...

Getting a hexadecimal number into a program via the command line

I can do this: int main(int argc, char** argv) { unsigned char cTest = 0xff; return 0; } But what's the right way to get a hexadecimal number into the program via the command line? unsigned char cTest = argv[1]; doesn't do the trick. That produces a initialization makes integer from pointer without a cast warning. ...

Do I need to cast the result of strtol to int?

The following code does not give a warning with g++ 4.1.1 and -Wall. int octalStrToInt(const std::string& s) { return strtol(s.c_str(), 0, 8); } I was expecting a warning because strtol returns a long int but my function is only returning a plain int. Might other compilers emit a warning here? Should I cast the return value ...

atol() v/s. strtol()

What is the difference between atol() & strtol()? According to their man pages, they seem to have the same effect as well as matching arguments: long atol(const char *nptr); long int strtol(const char *nptr, char **endptr, int base); In a generalized case, when I don't want to use the base argument (I just have decimal numbers), whi...