atoi()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Norbert Riedlin

    #16
    Re: atoi()

    "Jeff Schwab" <jeffplus@comca st.net> schrieb im Newsbeitrag
    news:f--dnWjwVfxAGJXdRV n-gQ@comcast.com. ..[color=blue]
    > Norbert Riedlin wrote:[color=green]
    > > "Jeff Schwab" <jeffplus@comca st.net> schrieb im Newsbeitrag
    > > news:o5-dnXnXd-tgfZrdRVn-jQ@comcast.com. ..
    > >[color=darkred]
    > >>template< typename N > // unsigned numeric type
    > >>N ato( char const* p, unsigned base =10 )
    > >>{
    > >> N result = N( );
    > >>
    > >> while( *p )
    > >> {
    > >> result = result * base + ( *p <= '9' ? *p - '0' : *p - 'a' + 10 );
    > >> ++p;
    > >> }
    > >>
    > >> return result;
    > >>}
    > >>[/color]
    > >
    > > You are asuming the ASCII character set? In EBCDIC '9' > 'a'.
    > >[/color]
    >
    > No assumption, just a mistake. That should have said:
    >
    > '0' <= *p && *p <= '9'
    >[/color]
    Ok, so you are asuming correct and all lowercase input?

    Bye Norbert



    Comment

    • Jeff Schwab

      #17
      Re: atoi()

      Norbert Riedlin wrote:[color=blue]
      > "Jeff Schwab" <jeffplus@comca st.net> schrieb im Newsbeitrag
      > news:f--dnWjwVfxAGJXdRV n-gQ@comcast.com. ..
      >[color=green]
      >>Norbert Riedlin wrote:
      >>[color=darkred]
      >>>"Jeff Schwab" <jeffplus@comca st.net> schrieb im Newsbeitrag
      >>>news:o5-dnXnXd-tgfZrdRVn-jQ@comcast.com. ..
      >>>
      >>>
      >>>>template< typename N > // unsigned numeric type
      >>>>N ato( char const* p, unsigned base =10 )
      >>>>{
      >>>> N result = N( );
      >>>>
      >>>> while( *p )
      >>>> {
      >>>> result = result * base + ( *p <= '9' ? *p - '0' : *p - 'a' + 10 );
      >>>> ++p;
      >>>> }
      >>>>
      >>>> return result;
      >>>>}
      >>>>
      >>>
      >>>You are asuming the ASCII character set? In EBCDIC '9' > 'a'.
      >>>[/color]
      >>
      >>No assumption, just a mistake. That should have said:
      >>
      >> '0' <= *p && *p <= '9'
      >>[/color]
      >
      > Ok, so you are asuming correct and all lowercase input?
      >
      > Bye Norbert[/color]

      Are you unable to read? Here's the part of my post you snipped.

      "The idea is this, but if you're looking for an improvement over atoi,
      you might consider validating the pointer, the base, and the digits."

      Comment

      Working...