Hi All,
I'm getting some weird results with my code, I'm trying to change a received string to a unsigned long, depending on the string received, I get different results, my code below
Serial.println("From the trigger");
Serial.println(str);
unsigned long B = atol(str.c_str());
unsigned long A = (long) strtol(str.c_str(),NULL,0);
Serial.println("this is A");
Serial.println(A);
Serial.println("this is B");
Serial.println(A);
WorkingString= String(A);
Serial.println("working string");
Serial.println(WorkingString);
Serial.println("and A");
Serial.println(A, HEX);
When I get a "working" value like 384311219 , the conversion is correct, but for something like 2291061678, the conversion becomes 2147483647
Any help would be appreciated please
Serial output below
From the trigger
2291061678
this is A
2147483647 *incorrect conversion*
this is B
2147483647 *incorrect conversion*
working string
2147483647
and A
7FFFFFFF
7FFFFFFF
From the trigger
384311219
this is A
384311219 *correct conversion*
this is B
384311219 *correct conversion*
working string
384311219
and A
16E81FB3
16E81FB3
2,291,061,678 is too big to store in a signed long.
Thanks, but any suggestions on what I can use, I thought unsigned long can handle
Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won’t store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1).
2.291.061.678 would indeed fit a 4-byte unsigned long just fine 
unsigned long B = atol(str.c_str());
If you want to convert to unsigned long, you need a function that can handle unsigned long
1 Like
I havent tried this on Arduino (it may not be in the core string library...
strtoul() function
This is so weird, even if I add this
unsigned long long B = 2291061678ul;
//long long B = atol(str.c_str());
unsigned long A = (long) strtol(str.c_str(),NULL,0);
Serial.println("this is A");
Serial.println(A);
Serial.println("this is B");
Serial.println(B);
I get the same, by the way it a Arduino pro mini
From the trigger
2291061678
this is A
2147483647
this is B
2147483647
working string
2147483647
and A
7FFFFFFF
7FFFFFFF
unsigned long A = (long) strtol(str.c_str(),NULL,0);
Why not use B but some function call which we can't see?
Why cast the damn thing to a singed int again?
Why not use a real null pointer 'nullptr'?
Serial.println("this is B");
Serial.println(A);
Why print A if you tell it's B?
Questions questions questions...
mmh me being stupid, this works, thanks all
unsigned long A = atol(str.c_str());
1 Like
i tried this
unsigned long A = atol(str.c_str());
it is still converting till 2,147,483,647 which is the upper limit of long data type. so just declaring a unsigned would only change the data type of the number stored in A but would not convert a number which is larger than
2,147,483,647.
still looking for converting a string with digits greater than 2,147,483,647 and less than 4,294,967,295 to unsigned long which actually increases the capacity of a number per say.
my observation thought i should share.