Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > converting a char* to long using atol does not work :(

Reply
Thread Tools

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

 
 
Gizmo
Guest
Posts: n/a
 
      09-03-2003
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


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-03-2003
Gizmo <(E-Mail Removed)> wrote in message
news:bj4qt8$qhd$(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      09-03-2003

"Gizmo" <(E-Mail Removed)> wrote in message
news:bj4qt8$qhd$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Guest
Posts: n/a
 
      09-03-2003

"Gizmo" <(E-Mail Removed)> wrote in message
news:bj4qt8$qhd$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-03-2003

"Gizmo" <(E-Mail Removed)> wrote in message news:bj4qt8$qhd$(E-Mail Removed)...

> 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...)



 
Reply With Quote
 
llewelly
Guest
Posts: n/a
 
      09-08-2003
"Gizmo" <(E-Mail Removed)> writes:

> 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++ .

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
Re: difference between casting and atol,atoi functions infobahn C Programming 1 02-15-2005 08:09 AM
Re: difference between casting and atol,atoi functions Olaf C Programming 0 02-15-2005 06:56 AM
string.atoi and string.atol broken? Mike Moum Python 19 01-27-2005 08:35 AM
what are the reverse functions of atof( ), atoi( ) and atol( ) ? Sharon C Programming 4 11-17-2003 10:13 AM



Advertisments