strtol

Convert long integer(decimal) to base 36 string (strtol inverted function in C)

穿精又带淫゛_ 提交于 2020-02-03 09:17:05
问题 I can use the strtol function for turning a base36 based value (saved as a string) into a long int : long int val = strtol("ABCZX123", 0, 36); Is there a standard function that allows the inversion of this? That is, to convert a long int val variable into a base36 string, to obtain "ABCZX123" again? 回答1: There's no standard function for this. You'll need to write your own one. Usage example: https://godbolt.org/z/MhRcNA const char digits[] =

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

人盡茶涼 提交于 2020-01-10 03:57:09
问题 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

strtol reusing param

心不动则不痛 提交于 2020-01-03 09:09:09
问题 This code seems to work as expected, populates an array of numbers using a single pointer #include <stdio.h> #include <ctype.h> #include <stdlib.h> int main(void) { int arr[4], count = 0, i; char *p, s[32] = " \t 10, 15 \n ,20, 25 , "; p = s; do { arr[count++] = (int)strtol(p, &p, 10); while (isspace(*p) || *p == ',') p++; } while (*p); for (i = 0; i < count; i++) { printf("%d\n", arr[i]); } return 0; } My question is: It is valid to use p as param1 (source) and &p as param 2 (address of the

Strtol second argument

柔情痞子 提交于 2019-12-25 08:36:06
问题 How does the second argument of strtol work? Here is what I tried: strtol(str, &ptr, 10) where ptr is a char * and str is a string. Now, If I pass in str as '34EF' , and print *ptr , it correctly gives me E , and *(ptr+1) gives me F , however if I print ptr , it gives me EF! Shouldn't printing ptr just result in a rubbish value like a hex address or something? 回答1: ptr is a pointer to the interior of a null terminated string. So given "34EF" it ends up pointing to the character 'E' and the

atol() v/s. strtol()

荒凉一梦 提交于 2019-12-17 08:09:58
问题 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), which function should I use? 回答1: strtol provides you with more flexibility, as it can actually tell you if the whole string was converted to an integer or not. atol

Why does string to int conversion not print the number 0 if its at first index [duplicate]

让人想犯罪 __ 提交于 2019-12-11 15:43:50
问题 This question already has answers here : Keep Leading zeros C (2 answers) keeping leading zeros in C [duplicate] (3 answers) Closed last year . I am wondering as to why when converting strings to int with either atoi or strtol it does not print the number 0 if its the first index, take this code for example char s[] = "0929784"; long temp = strtol(s, NULL, 10); printf("%li", temp); OUTPUT: 929784 is there a way to have the 0 print? 回答1: Use char s[] = "0929784"; size_t len = strlen(s); long

Using strtol to validate integer input in ANSI C

橙三吉。 提交于 2019-12-07 17:56:48
问题 I am new to programming and to C in general and am currently studying it at university. This is for an assignment so I would like to avoid direct answers but are more after tips or hints/pushes in the right direction. I am trying to use strtol to validate my keyboard input, more specifically, test whether the input is numeric. I have looked over other questions on here and other sites and I have followed instructions given to other users but it hasn't helped me. From what I have read/