I need to parse a decimal string representation of an unsigned 4 byte integer or long.
In my debugger I can see that when the 4 byte value string is 2943834865,
I get a result of 2147483647 (0x7fffffff) from atol,atoi and strtol .
fileChecksum = atol(Ptr2);
fileChecksum = strtol(Ptr2,&Ptr1,10);
The debugger:
Ptr2 -> Details:0x20002bd5 <g_bufferRead+13> "2943834865"
Name : fileChecksum
Details:2147483647
Default:2147483647
Decimal:2147483647
Hex:0x7fffffff
Binary:1111111111111111111111111111111
Octal:017777777777
I am guessing that this is because of an overflow from the signed value.
The fileChecksum I defined uint32_t and then as uint64_t with the same result.
Thanks
Solved! Go to Solution.
I also tried to enter the checksum string in hex format and got the same results 0x7fffffff
Today the checksum string was checksum=2939504859=0xAF3548DB.
I tried to enter "AF3548DB" with the strtol function set at base 16.
fileChecksum = strtol(Ptr2,&Ptr1,16);
There may be a linker or compiler setting.
I may just break the string into two parts but other places in my code I use the atoi,atol functions andthe problem may not have yet surfaced.
Why not use strtoul? This converts to unsigned long.
Thank you. Of course strtoul is what I was looking for. It works without problem. It makes sense to have strtoul and also strtol which I tried previously, but when you are used to the automatic conversion between signed and unsigned stuff in C, I did not think of it.
The no unsigned integers in Java is just crazy.
Thanks
Hi,
this function for example atol is defined as:
long int atol(const char *nptr);
Here GNU defined long max as 0x7FFFFFFF(2^31-1), so anything larger than that will out put 0x7FFFFFFF.
Have a great day,
Jun Zhang
-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!
- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------
I knew the size of a signed long but never thought they would think to cap results that actually fit into an unsigned long.
I guess I never noticed this and will have to check the places I use atoi & atol.
Thanks
I also tried to enter the checksum string in hex format and got the same results 0x7fffffff
Today the checksum string was checksum=2939504859=0xAF3548DB.
I tried to enter "AF3548DB" with the strtol function set at base 16.
fileChecksum = strtol(Ptr2,&Ptr1,16);
There may be a linker or compiler setting.
I may just break the string into two parts but other places in my code I use the atoi,atol functions andthe problem may not have yet surfaced.