05-30-2016 09:57 AM
Using LabWindowsCVI 2012, I have the following piece of code :
int sn;
int ret;
char str[16];
char *pEnd;
switch (event)
{
case EVENT_COMMIT:
SetWaitCursor (1);
GetCtrlVal(panel, FOST_FOSTSN, str);
sn = (int)strtol(str, &pEnd, 10);
...
}
str retrieved is "222" and I got the ERANGE error. Replace strtol() with strtoul() and the error gone.
Sadly, when I tried the following sample code from cplusplus.com and it works:
/* strtol example */ #include <stdio.h> /* printf */ #include <stdlib.h> /* strtol */ int main () { char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff"; char * pEnd; long int li1, li2, li3, li4; li1 = strtol (szNumbers,&pEnd,10); li2 = strtol (pEnd,&pEnd,16); li3 = strtol (pEnd,&pEnd,2); li4 = strtol (pEnd,NULL,0); printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4); return 0; }
It seems the behavior of strtol() is quite unpredictable.
05-31-2016 12:51 PM
Hello,
When you say “str received” is 222, do you mean that the input on the control is just ‘222’?
The ERANGE error tends to come up when the data type cannot handle the number of the input’s size size. The strtol function attempts to read a string into a long integer, but it returns an ERANGE error if the number is larger than 2,147,483,647. The strtoul function reads into an unsigned long integer which has a range of 4,294,967,295, which is twize the range of strtol function. If you are reading a number between these two ranges, that would explain why strtoul works but strtol does not.
This is a non-fatal error, so what value is saved to sn when the code runs?
06-01-2016 07:55 AM
Yes, the value entered in the control is "222". When it break I see the string "222" assigned to str variable but this still triggered the ERANGE error. After I replaced strtol() with strtoul(), it works fine and I got 222 exactly. This value is certainly not out of range
06-01-2016 08:11 AM
I just tried after the break in strtol(), I continue and check the value of sn, it is assigned 222. Not sure why this would trigger the ERANGE error.
06-01-2016 09:27 AM
06-02-2016 08:54 AM
I change the code to:
int sn;
long int longVal;
int ret;
char str[16];
char *pEnd;
switch (event)
{
case EVENT_COMMIT:
SetWaitCursor (1);
GetCtrlVal(panel, FOST_FOSTSN, str);
longVal = strtol(str, &pEnd, 10);
sn = (int)longVal;
I am still seeing the ERANGE error in strtol() call. See attachment.
06-02-2016 09:19 AM
Hello,
I feel that it might have something to do with your UI: what is the data type of your numeric control?
06-02-2016 09:35 AM
The control is a text box, not a numeric control. The data type is fixed as char *. Besides, the str variable is retrieved correctly and clearly has value "222". It can't be out of range. Why is it that strtoul() works fine if the UI is bad?
06-02-2016 09:44 AM
OK, my guess was wrong, didn't read carefully enough
06-02-2016 11:12 AM
Hello,
I am currently reproducing the code, and not running into any ERANGE errors. Below is what I have.
int sn; int ret; char str[16]; char *pEnd; switch (event) { case EVENT_COMMIT: SetWaitCursor(1); GetCtrlVal(panelHandle, PANEL_STRING, str); sn = (int)strtol(str, &pEnd, 10); SetCtrlVal(panelHandle, PANEL_RESULT, sn); SetWaitCursor (0); break; } return 0;
I imagine that you are working with a much larger project, however. Does your ERANGE error still occur when you have a small project with only the strtol function with the necessary inputs/outputs?