Convert string to long
-
Hi, How can I convert a string to long, so that the long value will be exactly the same value as the string? When I use the following code: CString str= "39436573472344"; long temp = atol(str); The value of temp is 2147483647. How can I get the value of temp to be the same as the string? Thanks.
-
Hi, How can I convert a string to long, so that the long value will be exactly the same value as the string? When I use the following code: CString str= "39436573472344"; long temp = atol(str); The value of temp is 2147483647. How can I get the value of temp to be the same as the string? Thanks.
The numeric value corrensponding to your string simply doesn't fit inside a variable of type
long
; in such cases theatol
returnsLONG_MAX
(i.e.2147483647
). You can use_strtoi64
function and a variable of type__int64
that holds values up to9223372036854775807
. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles] -
Hi, How can I convert a string to long, so that the long value will be exactly the same value as the string? When I use the following code: CString str= "39436573472344"; long temp = atol(str); The value of temp is 2147483647. How can I get the value of temp to be the same as the string? Thanks.
Your problem here is that the number is too large to be stored in 32 bit signed long. You'll need a 64 bit integer type for this one,
_int64 _atoi64( const char *string );
should do the trick or its UNICODE equivalent.Nothing is exactly what it seems but everything with seems can be unpicked.