converting a char* to long using atol does not work :(

G

Gizmo

Im not sure if this is a c or c++ question so I apologise if im in the wrong
place.



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. Is there any way around this ??



This is what im trying to do.



char *z = "15132770200";

long lShair = atol(z);



The answer it gives is -2047098984



If I reduce the string by one char like so



char *z = "1513277020";

long lShair = atol(z);



I get 1513277020



Is there any way around this ??



Thanks in advance



Gizmo
 
M

Mike Wahler

Gizmo said:
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
 
G

Guest

Gizmo said:
Im not sure if this is a c or c++ question so I apologise if im in the wrong
place.



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. Is there any way around this ??



This is what im trying to do.



char *z = "15132770200";

long lShair = atol(z);



The answer it gives is -2047098984



If I reduce the string by one char like so



char *z = "1513277020";

long lShair = atol(z);



I get 1513277020



Is there any way around this ??



Thanks in advance



Gizmo
your computer's long type is 32bit, isn't 64bit!
1513277020 < 32bit
15132770200 > 32bit
 
G

Guest

Gizmo said:
Im not sure if this is a c or c++ question so I apologise if im in the wrong
place.



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. Is there any way around this ??



This is what im trying to do.



char *z = "15132770200";

long lShair = atol(z);



The answer it gives is -2047098984



If I reduce the string by one char like so



char *z = "1513277020";

long lShair = atol(z);



I get 1513277020



Is there any way around this ??



Thanks in advance



Gizmo
your computer's long type is 32bit, isn't 64bit!
1513277020 < 32bit
15132770200 > 32bit
 
R

Ron Natalie

Gizmo said:
This is what im trying to do.



char *z = "15132770200";

long lShair = atol(z);

The number represented above is obviously too big to fit in a long
on your (presumably 32 bit matchine). What you wrote won't work
and nothing else will. If you exceed the size of long, you're going
to be out of luck.

As a matter of fact, what you wrote above is DANGEROUS. atol
is a ultimately stupidly defined function in the C standard. If you feed
it an overflowing input, there is no requirement for any specific behavior
in the failure.

strtol would be better, as would using the C++ formatted conversions.

There's no portable type lartger than long right now. You might want to switch
everything to doubles (use strtod, etc...)
 
L

llewelly

Gizmo said:
Im not sure if this is a c or c++ question so I apologise if im in the wrong
place. [snip]
However the number that I want to convert appears to be one digit to long to
be converted. Is there any way around this ??
This is what im trying to do.

char *z = "15132770200";

long lShair = atol(z);
[snip]

If this is a C question you can use long long:

long long lShair= atoll(z);

However atoll has undefined behavior if there is overflow or
underflow. So you are better off with strtoll:

long long lShair= strtoll(z, NULL, 10);

long long, atoll, and strtoll are new with C99. They are not part of
C89 or C++ .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top