Gizmo <> wrote in message
news:bj4qt8$qhd$...
> Im not sure if this is a c or c++ question
It's both.
> so I apologise if im in the wrong
> place.
Your question is certainly topical here.
>
> I want to convert a char* to a long.
>
> However the number that I want to convert appears to be one digit to long
to
> be converted.
The largest value guaranteed to be representable by
type 'long' is 2147483647
>Is there any way around this ??
Use a larger type. If one is not available with
your implementation, you'll need a custom 'bignum'
class.
>
> This is what im trying to do.
>
> char *z = "15132770200";
>
> long lShair = atol(z);
>
> The answer it gives is -2047098984
The answer could have been anything. If 'atol()' tries
to convert an out-of-range value, the resultant behavior
is undefined. This is why one generally should not use 'atol()'
at all.
> If I reduce the string by one char like so
>
> char *z = "1513277020";
>
> long lShair = atol(z);
>
> I get 1513277020
That value is within the guaranteed range of type 'long',
so it works.
I recommend you not use 'atol()' at all, but use 'strtol()'
instead, which can tell you for sure, in a defined way, if
the value being converted is out of range.
> Is there any way around this ??
>
> Thanks in advance
>
> Gizmo
-Mike