strtol

Basics of strtol?

孤者浪人 提交于 2019-11-30 03:55:39
问题 I am really confused. I have to be missing something rather simple but nothing I am reading about strtol() is making sense. Can someone spell it out for me in a really basic way, as well as give an example for how I might get something like the following to work? string input = getUserInput; int numberinput = strtol(input,?,?); 回答1: The first argument is the string. It has to be passed in as a C string, so if you have a std::string use .c_str() first. The second argument is optional, and

Why can't you just check if errno is equal to ERANGE?

孤街醉人 提交于 2019-11-29 14:02:50
I've been trying to properly convert a char array to a long with strtol , check if there was an overflow or underflow and then do an int cast on the long. Along the way, I've noticed a lot of code that looks like this if ((result == LONG_MAX || result == LONG_MIN) && errno == ERANGE) { // Handle the error } Why can you not just say if(errno == ERANGE) { // Handle the error } From my understanding, if an underflow or overflow occur, errno is set to ERANGE in both cases. So is the former really necessary? Could checking ERANGE alone be problematic? This how my code looks as of now char *endPtr;

Convert a hexadecimal string to an integer efficiently in C?

ぃ、小莉子 提交于 2019-11-27 08:02:49
In C, what is the most efficient way to convert a string of hex digits into a binary unsigned int or unsigned long ? For example, if I have 0xFFFFFFFE , I want an int with the base10 value 4294967294 . Patrick You want strtol or strtoul . See also the Unix man page Edit: Now compatible with MSVC, C++ and non-GNU compilers (see end). The question was "most efficient way." The OP doesn't specify platform, he could be compiling for a RISC based ATMEL chip with 256 bytes of flash storage for his code. For the record, and for those (like me), who appreciate the difference between "the easiest way"

Convert a hexadecimal string to an integer efficiently in C?

回眸只為那壹抹淺笑 提交于 2019-11-26 13:50:48
问题 In C, what is the most efficient way to convert a string of hex digits into a binary unsigned int or unsigned long ? For example, if I have 0xFFFFFFFE , I want an int with the base10 value 4294967294 . 回答1: You want strtol or strtoul. See also the Unix man page 回答2: Edit: Now compatible with MSVC, C++ and non-GNU compilers (see end). The question was "most efficient way." The OP doesn't specify platform, he could be compiling for a RISC based ATMEL chip with 256 bytes of flash storage for his