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
"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