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

Discussion in 'C++' started by Gizmo, Sep 3, 2003.

  1. Gizmo

    Gizmo Guest

    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
     
    Gizmo, Sep 3, 2003
    #1
    1. Advertising

  2. Gizmo

    Mike Wahler Guest

    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
     
    Mike Wahler, Sep 3, 2003
    #2
    1. Advertising

  3. Gizmo

    Guest Guest

    "Gizmo" <> wrote in message
    news:bj4qt8$qhd$...
    > 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
     
    Guest, Sep 3, 2003
    #3
  4. Gizmo

    Guest Guest

    "Gizmo" <> wrote in message
    news:bj4qt8$qhd$...
    > 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
     
    Guest, Sep 3, 2003
    #4
  5. Gizmo

    Ron Natalie Guest

    "Gizmo" <> wrote in message news:bj4qt8$qhd$...

    > 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...)
     
    Ron Natalie, Sep 3, 2003
    #5
  6. Gizmo

    llewelly Guest

    "Gizmo" <> 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++ .
     
    llewelly, Sep 8, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sharon
    Replies:
    4
    Views:
    1,077
    CBFalconer
    Nov 17, 2003
  2. Mike Moum

    string.atoi and string.atol broken?

    Mike Moum, Jan 25, 2005, in forum: Python
    Replies:
    19
    Views:
    695
    Peter Otten
    Jan 27, 2005
  3. silangdon

    Converting long long to char

    silangdon, Mar 21, 2005, in forum: C Programming
    Replies:
    8
    Views:
    431
    Richard Bos
    Mar 22, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,154
    Ian Collins
    May 9, 2006
  5. Oliver Graeser
    Replies:
    10
    Views:
    607
    Oliver Graeser
    Sep 26, 2008
Loading...

Share This Page