Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #103661

Re: better atoi

From Joe Pfeiffer <pfeiffer@cs.nmsu.edu>
Newsgroups comp.lang.c
Subject Re: better atoi
Date 2017-02-10 22:36 -0700
Organization A noiseless patient Spider
Message-ID <1bfujli90k.fsf@pfeifferfamily.net> (permalink)
References <853138f9-54d1-435f-90d5-ef75a38e65bf@googlegroups.com> <1b1sv5k98w.fsf@pfeifferfamily.net> <c74e3f79-52b6-4c5a-b000-167ea48b26f0@googlegroups.com>

Show all headers | View raw


fir <profesor.fir@gmail.com> writes:

> W dniu piątek, 10 lutego 2017 22:48:38 UTC+1 użytkownik Joe Pfeiffer napisał:
>> fir <profesor.fir@gmail.com> writes:
>> >
>> > http://www.cplusplus.com/reference/cstdlib/strtol/
>> >
>> > it is said that strtol sets  errno to ERANGE but is
>> > not mentioned with wrong value, so im not sure if
>> > it can be uniformly checked (probably yest though 
>> > but only not noted (i not checked it though))
>> 
>> It says it returns LONG_MAX or LONG_MIN in this case (so it appears it
>> is possible that if the string does in fact represent one of those
>> values, you can't reliably detect the conversion was successful -- as
>> we've seen in other places, we can't rely on ERRNO being 0 on success.
>> We'd sort of hope for it in this function...).
>> 
>> > even if this strtol works ok there is also a doubt
>> > how it shoud treat 'non pure' numbers (like "  0034aaa" 
>> > or "  035 aaa")
>> > should it rise an error or just return 34 - not fully 
>> > clear maybe (maybe clear maybe not it needs a bit of 
>> > insight)
>> 
>> No, this is clearly stated in the page you cited:
>> 
>>     The function first discards as many whitespace characters as
>>     necessary until the first non-whitespace character is found. Then,
>>     starting from this character, takes as many characters as possible
>>     that are valid following a syntax that depends on the base
>>     parameter, and interprets them as a numerical value. Finally, a
>>     pointer to the first character following the integer representation
>>     in str is stored in the object pointed by endptr.
>> 
>> (and it goes on to define the syntax)
>
> i was reading that but still it is not clear to me from this if it
> should take " 0034aaa" as 34 or this is a error input, same with " 035
> aaa"

If the radix is less that 11, 'a' is not a numeric character.  Therefore
the result with " 0034aaa" is the conversion of "0034" (and what that
result is depends on the radix) for radixes up to 10, and the conversion
of "0034aaa" for radixes from 11 to 36.  ' ' is not a numeric character
in any radix, so  the result is the conversion of "035" (again, the
result depends on the radix).

> problem is a bit more complex, as i mentioned i would like to get a
> function that would take string and returned
> integer yet with a couple of information of errors (if no number, or
> out of range) and also on succes (if it accepts
> dirty numbers as input)

Near I can tell, strtol() does exactly what you're looking for.

> this errno could be used as this information though i never used it im
> not sure if it can be context dependant value or only some globally
> predefined errors
>
> im not sure if in fact this present implementation is 
> ok in those mentioned regard or it is really flawed 
> for some

It appears to do what you want.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

better atoi fir <profesor.fir@gmail.com> - 2017-02-10 12:24 -0800
  Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-10 12:35 -0800
  Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 14:48 -0700
    Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-10 14:12 -0800
      Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 22:36 -0700
        Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-11 01:07 -0800
          Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 10:10 -0700
            Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-11 09:26 -0800
              Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 15:36 -0700
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 00:18 -0800
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 00:31 -0800
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 03:47 -0800
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 04:22 -0800
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 06:08 -0800
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 06:13 -0800
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 07:31 -0800
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 08:02 -0800
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 08:08 -0800
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 08:17 -0800
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 08:23 -0800
                Re: better atoi Robert Latest <boblatest@yahoo.com> - 2017-02-19 11:11 +0000
            Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 17:33 +0000
              Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 15:35 -0700
                Re: better atoi "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-11 17:52 -0500
                Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 18:53 -0700
    Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-10 22:50 +0000
      Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 21:59 -0700
        Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 12:08 +0000
          Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 09:39 -0700
            Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 17:26 +0000
              Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 15:30 -0700
                Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 22:58 +0000
                Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-11 18:48 -0700
                Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-12 02:16 +0000
                Re: better atoi "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-12 09:57 -0500
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 07:27 -0800
                Re: better atoi jameskuyper@verizon.net - 2017-02-12 08:44 -0800
                Re: better atoi fir <profesor.fir@gmail.com> - 2017-02-12 09:15 -0800
                Re: better atoi Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-02-12 09:44 -0800
                Re: better atoi BartC <bc@freeuk.com> - 2017-02-12 18:32 +0000
                Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-12 18:56 +0000
                Re: better atoi BartC <bc@freeuk.com> - 2017-02-12 19:18 +0000
                Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-13 00:16 +0000
                Re: better atoi BartC <bc@freeuk.com> - 2017-02-13 01:56 +0000
                Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-13 10:44 +0000
                Re: better atoi BartC <bc@freeuk.com> - 2017-02-13 11:38 +0000
                Re: better atoi Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-13 14:37 +0000
                Re: better atoi BartC <bc@freeuk.com> - 2017-02-13 15:00 +0000
                Re: better atoi Keith Thompson <kst-u@mib.org> - 2017-02-12 11:15 -0800
                Re: better atoi BartC <bc@freeuk.com> - 2017-02-12 19:25 +0000
                Re: better atoi Keith Thompson <kst-u@mib.org> - 2017-02-12 11:54 -0800
                Re: better atoi supercat@casperkitty.com - 2017-02-12 13:13 -0800
    Re: better atoi Keith Thompson <kst-u@mib.org> - 2017-02-10 15:16 -0800
      Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 22:01 -0700
        Re: better atoi Keith Thompson <kst-u@mib.org> - 2017-02-11 01:30 -0800
      Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 22:26 -0700
    Re: better atoi Nick Bowler <nbowler@draconx.ca> - 2017-02-11 00:26 +0000
      Re: better atoi Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-02-10 22:32 -0700
    Re: better atoi "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-19 11:10 -0500

csiph-web